A:添加 env 开发路由(有待调试)
This commit is contained in:
1 parent
77fb95b967
commit
a345134e04
7 files changed
+72
-70
No files matched your search
@@ -0,0 +1,40 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"rear/internal/container"
|
||||||
|
"rear/internal/utils/tools"
|
||||||
|
"rear/pkg/logger"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DevImageHandler struct {
|
||||||
|
container *container.Container
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDevImageHandler(container *container.Container) *DevImageHandler {
|
||||||
|
return &DevImageHandler{container: container}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetExif 获取图片的EXIF信息
|
||||||
|
func (h *DevImageHandler) GetExif(c *gin.Context) {
|
||||||
|
// 从 query 参数中获取图片路径
|
||||||
|
logger.Warn("GetExif called, processing request...")
|
||||||
|
imagePath := c.Query("image_path")
|
||||||
|
|
||||||
|
result := fmt.Sprintf("File generation done. %s\n", imagePath)
|
||||||
|
logger.Warn(result)
|
||||||
|
if imagePath == "" {
|
||||||
|
c.JSON(400, gin.H{"error": "image_path query parameter is required"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 调用工具函数获取 EXIF 数据
|
||||||
|
exifData, err := tools.GetExifData(c, imagePath)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(500, gin.H{"error": "failed to get EXIF data"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, gin.H{"exif_data": exifData})
|
||||||
|
}
|
||||||
@@ -11,16 +11,16 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Handler struct {
|
type LibraryHandler struct {
|
||||||
container *container.Container
|
container *container.Container
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLibraryHandler(container *container.Container) *Handler {
|
func NewLibraryHandler(container *container.Container) *LibraryHandler {
|
||||||
return &Handler{container: container}
|
return &LibraryHandler{container: container}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLibrary 获取存储
|
// GetLibrary 获取存储
|
||||||
func (h *Handler) GetLibrary(c *gin.Context) {
|
func (h *LibraryHandler) GetLibrary(c *gin.Context) {
|
||||||
libraries, err := h.container.LibraryRepo.GetAllLibrary()
|
libraries, err := h.container.LibraryRepo.GetAllLibrary()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to get all libraries: %v", err)
|
log.Printf("Failed to get all libraries: %v", err)
|
||||||
@@ -39,7 +39,7 @@ func (h *Handler) GetLibrary(c *gin.Context) {
|
|||||||
Data: libraries,
|
Data: libraries,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
func (h *Handler) AddLibrary(c *gin.Context) {
|
func (h *LibraryHandler) AddLibrary(c *gin.Context) {
|
||||||
type AddLibraryRequest struct {
|
type AddLibraryRequest struct {
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
}
|
}
|
||||||
@@ -85,7 +85,7 @@ func (h *Handler) AddLibrary(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) UpdateLibrary(c *gin.Context) {
|
func (h *LibraryHandler) UpdateLibrary(c *gin.Context) {
|
||||||
type UpdateLibraryRequest struct {
|
type UpdateLibraryRequest struct {
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
IsEnable bool `json:"is_enable"`
|
IsEnable bool `json:"is_enable"`
|
||||||
@@ -116,7 +116,7 @@ func (h *Handler) UpdateLibrary(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) DeleteLibrary(c *gin.Context) {
|
func (h *LibraryHandler) DeleteLibrary(c *gin.Context) {
|
||||||
path := c.Query("path")
|
path := c.Query("path")
|
||||||
if strings.TrimSpace(path) == "" {
|
if strings.TrimSpace(path) == "" {
|
||||||
c.JSON(http.StatusBadRequest, model.Response{
|
c.JSON(http.StatusBadRequest, model.Response{
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func SetupRoutes(r *gin.Engine, contain *container.Container) {
|
|||||||
|
|
||||||
// 资料库处理
|
// 资料库处理
|
||||||
libraryHandler := handler.NewLibraryHandler(contain)
|
libraryHandler := handler.NewLibraryHandler(contain)
|
||||||
|
devImageHandler := handler.NewDevImageHandler(contain)
|
||||||
// API版本组
|
// API版本组
|
||||||
v1 := r.Group("/api/v1")
|
v1 := r.Group("/api/v1")
|
||||||
{
|
{
|
||||||
@@ -40,4 +40,10 @@ func SetupRoutes(r *gin.Engine, contain *container.Container) {
|
|||||||
library.DELETE("", libraryHandler.DeleteLibrary)
|
library.DELETE("", libraryHandler.DeleteLibrary)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 开发组
|
||||||
|
dev := r.Group("/dev")
|
||||||
|
{
|
||||||
|
// 处理图片格式
|
||||||
|
dev.GET("/tool/exiftool/get_exif", devImageHandler.GetExif)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
package tools
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ========== 使用示例 ==========
|
|
||||||
|
|
||||||
func Example() {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
// 1. 简单的图片处理
|
|
||||||
err := ResizeImage(ctx, "input.jpg", "output.jpg", 800, 600)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Resize failed: %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. 获取图片信息
|
|
||||||
//info, err := GetImageInfo(ctx, "photo.jpg")
|
|
||||||
//if err != nil {
|
|
||||||
// fmt.Printf("Get info failed: %v\n", err)
|
|
||||||
//}
|
|
||||||
//fmt.Printf("Image: %dx%d, Format: %s\n", info.Width, info.Height, info.Format)
|
|
||||||
|
|
||||||
// 3. 处理 EXIF
|
|
||||||
exifData, err := GetExifData(ctx, "photo.jpg")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Get EXIF failed: %v\n", err)
|
|
||||||
}
|
|
||||||
fmt.Printf("EXIF: %v\n", exifData)
|
|
||||||
|
|
||||||
// 4. 在 goroutine 中使用(并发安全)
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
wg.Add(1)
|
|
||||||
go func(index int) {
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
// 每个 goroutine 可以安全地调用这些函数
|
|
||||||
input := fmt.Sprintf("img%d.jpg", index)
|
|
||||||
output := fmt.Sprintf("thumb%d.jpg", index)
|
|
||||||
|
|
||||||
// 使用带超时的 context
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
if err := ResizeImage(ctx, input, output, 200, 200); err != nil {
|
|
||||||
fmt.Printf("Error processing %s: %v\n", input, err)
|
|
||||||
}
|
|
||||||
}(i)
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
}
|
|
||||||
+7
-3
@@ -3,9 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/gin-contrib/cors"
|
|
||||||
"github.com/gin-contrib/pprof"
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -19,6 +16,10 @@ import (
|
|||||||
"rear/pkg/logger"
|
"rear/pkg/logger"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-contrib/cors"
|
||||||
|
"github.com/gin-contrib/pprof"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -44,6 +45,9 @@ func main() {
|
|||||||
// 初始化基础服务(启动写操作处理协程)
|
// 初始化基础服务(启动写操作处理协程)
|
||||||
repositories.InitBaseService()
|
repositories.InitBaseService()
|
||||||
|
|
||||||
|
// 初始化外部(CLI)工具管理
|
||||||
|
// utils.EnsureInitialized()
|
||||||
|
|
||||||
//if err := initDatabase(); err != nil {
|
//if err := initDatabase(); err != nil {
|
||||||
// logger.Fatalf("Failed to initialize database: %v", err)
|
// logger.Fatalf("Failed to initialize database: %v", err)
|
||||||
//}
|
//}
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
package imgutils
|
package imgutils
|
||||||
|
|
||||||
CommonFormat =: ["JPG", "JPEG", "PNG"]
|
var (
|
||||||
UncommonFormat =:["BMP", "TIFF", "TIF", "WebP", "HEIC","HEIF","GIF","APNG","SVG"]
|
// 常规格式
|
||||||
RawFormat = ["CR2", "NEF", "ARW", "ORF", "RAF", "DNG", "PEF", "SR2", "RW2", "X3F"]
|
CommonFormat = []string{"JPG", "JPEG", "PNG"}
|
||||||
|
// 需要特殊处理的格式
|
||||||
|
UncommonFormat = []string{"BMP", "TIFF", "TIF", "WebP", "HEIC", "HEIF", "GIF", "APNG", "SVG"}
|
||||||
|
// Raw 格式
|
||||||
|
RawFormat = []string{"CR2", "NEF", "ARW", "ORF", "RAF", "DNG", "PEF", "SR2", "RW2", "X3F"}
|
||||||
|
)
|
||||||
|
|
||||||
|
// 获取指定文件夹下所有的文件夹
|
||||||
|
//
|
||||||
Binary file not shown.
Reference in new issue
Block a user