generate thumbnails
This commit is contained in:
@@ -12,9 +12,10 @@ import (
|
||||
)
|
||||
|
||||
type FileHandler struct {
|
||||
files *service.FileService
|
||||
urls *service.URLService
|
||||
ocr *service.OCRService
|
||||
files *service.FileService
|
||||
urls *service.URLService
|
||||
ocr *service.OCRService
|
||||
conversion *service.ConversionService
|
||||
}
|
||||
|
||||
func (h *FileHandler) Upload(c *gin.Context) {
|
||||
@@ -40,6 +41,10 @@ func (h *FileHandler) Upload(c *gin.Context) {
|
||||
|
||||
h.ocr.Enqueue(result.ID, result.Path)
|
||||
|
||||
if service.IsConvertible(result.MimeType) {
|
||||
h.conversion.Enqueue(result.ID, result.Path, result.MimeType)
|
||||
}
|
||||
|
||||
results = append(results, gin.H{
|
||||
"id": result.ID,
|
||||
"name": result.Name,
|
||||
@@ -252,3 +257,56 @@ func (h *FileHandler) ListFilesByParent(c *gin.Context) {
|
||||
}
|
||||
api.Success(c, files)
|
||||
}
|
||||
|
||||
func (h *FileHandler) GetThumbnails(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
thumbnails, err := h.files.GetThumbnailsByFileID(id)
|
||||
if err != nil {
|
||||
api.Error(c, http.StatusInternalServerError, "DB_ERROR", "Failed to fetch thumbnails")
|
||||
return
|
||||
}
|
||||
|
||||
type thumbnailResponse struct {
|
||||
ID string `json:"id"`
|
||||
PageNumber int `json:"pageNumber"`
|
||||
ResolutionLabel string `json:"resolutionLabel"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
URL string `json:"url"`
|
||||
MimeType string `json:"mimeType"`
|
||||
}
|
||||
|
||||
resp := make([]thumbnailResponse, len(thumbnails))
|
||||
for i, t := range thumbnails {
|
||||
resp[i] = thumbnailResponse{
|
||||
ID: t.ID,
|
||||
PageNumber: t.PageNumber,
|
||||
ResolutionLabel: t.ResolutionLabel,
|
||||
Width: t.Width,
|
||||
Height: t.Height,
|
||||
URL: h.urls.GenerateThumbnailURL(t.ID),
|
||||
MimeType: t.MimeType,
|
||||
}
|
||||
}
|
||||
|
||||
api.Success(c, resp)
|
||||
}
|
||||
|
||||
func (h *FileHandler) ServeThumbnail(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
exp, _ := strconv.ParseInt(c.Query("expires"), 10, 64)
|
||||
sig := c.Query("sig")
|
||||
|
||||
if !h.urls.Validate(id, sig, exp) {
|
||||
api.Error(c, http.StatusForbidden, "FORBIDDEN", "Invalid or expired link")
|
||||
return
|
||||
}
|
||||
|
||||
storagePath, err := h.files.GetThumbnailStoragePath(id)
|
||||
if err != nil {
|
||||
api.Error(c, http.StatusNotFound, "THUMBNAIL_NOT_FOUND", "Thumbnail not found")
|
||||
return
|
||||
}
|
||||
|
||||
c.File(path.Clean(storagePath))
|
||||
}
|
||||
|
||||
@@ -12,9 +12,9 @@ type Handler struct {
|
||||
Auth *auth.AuthHandler
|
||||
}
|
||||
|
||||
func New(fileSvc *service.FileService, ocrSvc *service.OCRService, urlSvc *service.URLService, authHandler *auth.AuthHandler) *Handler {
|
||||
func New(fileSvc *service.FileService, ocrSvc *service.OCRService, urlSvc *service.URLService, authHandler *auth.AuthHandler, conversionSvc *service.ConversionService) *Handler {
|
||||
return &Handler{
|
||||
File: &FileHandler{files: fileSvc, urls: urlSvc, ocr: ocrSvc},
|
||||
File: &FileHandler{files: fileSvc, urls: urlSvc, ocr: ocrSvc, conversion: conversionSvc},
|
||||
OCR: &OCRHandler{ocr: ocrSvc, files: fileSvc},
|
||||
Health: &HealthHandler{ocr: ocrSvc},
|
||||
Auth: authHandler,
|
||||
|
||||
@@ -34,4 +34,8 @@ func SetupRoutes(r *gin.Engine, h *Handler, authMiddleware *auth.AuthService) {
|
||||
|
||||
protected.POST("/ocr/jobs", h.OCR.CreateJob)
|
||||
protected.GET("/ocr/jobs/:id", h.OCR.GetJobStatus)
|
||||
|
||||
protected.GET("/files/:id/thumbnails", h.File.GetThumbnails)
|
||||
|
||||
api.GET("/thumbnails/:id", h.File.ServeThumbnail)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user