fix 401 endpoint - signed endpoint
This commit is contained in:
@@ -55,6 +55,8 @@ func (h *FileHandler) Upload(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *FileHandler) List(c *gin.Context) {
|
func (h *FileHandler) List(c *gin.Context) {
|
||||||
|
thumbnailQuality := c.Query("thumbnail")
|
||||||
|
|
||||||
files, err := h.files.List()
|
files, err := h.files.List()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ERROR List files: %v", err)
|
log.Printf("ERROR List files: %v", err)
|
||||||
@@ -71,6 +73,7 @@ func (h *FileHandler) List(c *gin.Context) {
|
|||||||
type fileResponse struct {
|
type fileResponse struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
|
ThumbnailURL string `json:"thumbnailUrl,omitempty"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Size int64 `json:"size"`
|
Size int64 `json:"size"`
|
||||||
Tags []tagResponse `json:"tags"`
|
Tags []tagResponse `json:"tags"`
|
||||||
@@ -84,21 +87,27 @@ func (h *FileHandler) List(c *gin.Context) {
|
|||||||
|
|
||||||
resp := make([]fileResponse, len(files))
|
resp := make([]fileResponse, len(files))
|
||||||
for i, f := range files {
|
for i, f := range files {
|
||||||
|
|
||||||
tags := []tagResponse{}
|
tags := []tagResponse{}
|
||||||
|
|
||||||
for _, tag := range f.Tags {
|
for _, tag := range f.Tags {
|
||||||
|
|
||||||
tags = append(tags, tagResponse{
|
tags = append(tags, tagResponse{
|
||||||
ID: tag.ID,
|
ID: tag.ID,
|
||||||
TagName: tag.Name,
|
TagName: tag.Name,
|
||||||
TagType: tag.TagType,
|
TagType: tag.TagType,
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
downloadURL := h.urls.GenerateDownloadURL(f.ID)
|
||||||
|
thumbURL := downloadURL
|
||||||
|
if thumbnailQuality != "" {
|
||||||
|
if best := h.files.GetBestThumbnail(f.ID, thumbnailQuality); best != nil {
|
||||||
|
thumbURL = h.urls.GenerateThumbnailURL(best.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
resp[i] = fileResponse{
|
resp[i] = fileResponse{
|
||||||
ID: f.ID,
|
ID: f.ID,
|
||||||
URL: h.urls.GenerateDownloadURL(f.ID),
|
URL: downloadURL,
|
||||||
|
ThumbnailURL: thumbURL,
|
||||||
Name: f.Name,
|
Name: f.Name,
|
||||||
Size: f.Size,
|
Size: f.Size,
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
@@ -135,21 +144,75 @@ func (h *FileHandler) Download(c *gin.Context) {
|
|||||||
|
|
||||||
func (h *FileHandler) Get(c *gin.Context) {
|
func (h *FileHandler) Get(c *gin.Context) {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
|
thumbnailQuality := c.Query("thumbnail")
|
||||||
|
|
||||||
file, err := h.files.Get(id)
|
file, err := h.files.Get(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.Error(c, http.StatusNotFound, "FILE_NOT_FOUND", "File not found")
|
api.Error(c, http.StatusNotFound, "FILE_NOT_FOUND", "File not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type tagResponse struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
TagName string `json:"tag_name"`
|
||||||
|
TagType string `json:"tag_type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
tags := []tagResponse{}
|
||||||
|
for _, tag := range file.Tags {
|
||||||
|
tags = append(tags, tagResponse{
|
||||||
|
ID: tag.ID,
|
||||||
|
TagName: tag.Name,
|
||||||
|
TagType: tag.TagType,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
downloadURL := h.urls.GenerateDownloadURL(file.ID)
|
||||||
|
thumbURL := downloadURL
|
||||||
|
if thumbnailQuality != "" {
|
||||||
|
if best := h.files.GetBestThumbnail(file.ID, thumbnailQuality); best != nil {
|
||||||
|
thumbURL = h.urls.GenerateThumbnailURL(best.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dbThumbnails, _ := h.files.GetThumbnailsByFileID(file.ID)
|
||||||
|
thumbnails := make([]thumbnailResponse, len(dbThumbnails))
|
||||||
|
for i, t := range dbThumbnails {
|
||||||
|
thumbnails[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, gin.H{
|
api.Success(c, gin.H{
|
||||||
"id": file.ID,
|
"id": file.ID,
|
||||||
"name": file.Name,
|
"name": file.Name,
|
||||||
"url": h.urls.GenerateDownloadURL(file.ID),
|
"url": downloadURL,
|
||||||
"size": file.Size,
|
"thumbnailUrl": thumbURL,
|
||||||
"mimeType": file.MimeType,
|
"size": file.Size,
|
||||||
"createdAt": file.CreatedAt,
|
"mimeType": file.MimeType,
|
||||||
"updatedAt": file.UpdatedAt,
|
"tags": tags,
|
||||||
"ocrText": file.OcrText,
|
"createdAt": file.CreatedAt,
|
||||||
|
"updatedAt": file.UpdatedAt,
|
||||||
|
"ocrText": file.OcrText,
|
||||||
|
"isFolder": file.IsFolder,
|
||||||
|
"parentFileID": file.ParentFileID,
|
||||||
|
"thumbnails": thumbnails,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,12 +313,71 @@ func (h *FileHandler) CreateFolder(c *gin.Context) {
|
|||||||
|
|
||||||
func (h *FileHandler) ListFilesByParent(c *gin.Context) {
|
func (h *FileHandler) ListFilesByParent(c *gin.Context) {
|
||||||
parentID := c.Param("id")
|
parentID := c.Param("id")
|
||||||
|
thumbnailQuality := c.Query("thumbnail")
|
||||||
|
|
||||||
files, err := h.files.ListFilesByParentID(parentID)
|
files, err := h.files.ListFilesByParentID(parentID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.Error(c, http.StatusInternalServerError, "DB_ERROR", "Failed to list files in folder")
|
api.Error(c, http.StatusInternalServerError, "DB_ERROR", "Failed to list files in folder")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
api.Success(c, files)
|
|
||||||
|
type tagResponse struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
TagName string `json:"tag_name"`
|
||||||
|
TagType string `json:"tag_type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type fileResponse struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
ThumbnailURL string `json:"thumbnailUrl,omitempty"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Size int64 `json:"size"`
|
||||||
|
Tags []tagResponse `json:"tags"`
|
||||||
|
CreatedAt string `json:"createdAt"`
|
||||||
|
MimeType string `json:"mimeType"`
|
||||||
|
OcrText string `json:"ocrText,omitempty"`
|
||||||
|
ParentFileID string `json:"parentFileID,omitempty"`
|
||||||
|
IsFolder bool `json:"isFolder"`
|
||||||
|
UpdatedAt string `json:"updatedAt"`
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := make([]fileResponse, len(files))
|
||||||
|
for i, f := range files {
|
||||||
|
tags := []tagResponse{}
|
||||||
|
for _, tag := range f.Tags {
|
||||||
|
tags = append(tags, tagResponse{
|
||||||
|
ID: tag.ID,
|
||||||
|
TagName: tag.Name,
|
||||||
|
TagType: tag.TagType,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
downloadURL := h.urls.GenerateDownloadURL(f.ID)
|
||||||
|
thumbURL := downloadURL
|
||||||
|
if thumbnailQuality != "" {
|
||||||
|
if best := h.files.GetBestThumbnail(f.ID, thumbnailQuality); best != nil {
|
||||||
|
thumbURL = h.urls.GenerateThumbnailURL(best.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp[i] = fileResponse{
|
||||||
|
ID: f.ID,
|
||||||
|
URL: downloadURL,
|
||||||
|
ThumbnailURL: thumbURL,
|
||||||
|
Name: f.Name,
|
||||||
|
Size: f.Size,
|
||||||
|
Tags: tags,
|
||||||
|
CreatedAt: f.CreatedAt,
|
||||||
|
ParentFileID: f.ParentFileID,
|
||||||
|
OcrText: f.OcrText,
|
||||||
|
IsFolder: f.IsFolder,
|
||||||
|
UpdatedAt: f.UpdatedAt,
|
||||||
|
MimeType: f.MimeType,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
api.Success(c, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *FileHandler) GetThumbnails(c *gin.Context) {
|
func (h *FileHandler) GetThumbnails(c *gin.Context) {
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ func SetupRoutes(r *gin.Engine, h *Handler, authMiddleware *auth.AuthService) {
|
|||||||
api.POST("/auth/login", h.Auth.Login)
|
api.POST("/auth/login", h.Auth.Login)
|
||||||
api.POST("/auth/refresh", h.Auth.Refresh)
|
api.POST("/auth/refresh", h.Auth.Refresh)
|
||||||
api.POST("/auth/logout", h.Auth.Logout)
|
api.POST("/auth/logout", h.Auth.Logout)
|
||||||
|
api.GET("/files/download/:id", h.File.Download)
|
||||||
|
api.GET("/thumbnails/:id", h.File.ServeThumbnail)
|
||||||
|
|
||||||
// Protected
|
// Protected
|
||||||
protected := api.Group("")
|
protected := api.Group("")
|
||||||
@@ -25,7 +27,6 @@ func SetupRoutes(r *gin.Engine, h *Handler, authMiddleware *auth.AuthService) {
|
|||||||
protected.POST("/files/folders", h.File.CreateFolder)
|
protected.POST("/files/folders", h.File.CreateFolder)
|
||||||
protected.GET("/files/folders", h.File.ListFolders)
|
protected.GET("/files/folders", h.File.ListFolders)
|
||||||
protected.GET("/files/folders/:id/files", h.File.ListFilesByParent)
|
protected.GET("/files/folders/:id/files", h.File.ListFilesByParent)
|
||||||
protected.GET("/files/download/:id", h.File.Download)
|
|
||||||
protected.DELETE("/files/:id", h.File.Delete)
|
protected.DELETE("/files/:id", h.File.Delete)
|
||||||
protected.GET("/files/:id", h.File.Get)
|
protected.GET("/files/:id", h.File.Get)
|
||||||
|
|
||||||
@@ -36,6 +37,4 @@ func SetupRoutes(r *gin.Engine, h *Handler, authMiddleware *auth.AuthService) {
|
|||||||
protected.GET("/ocr/jobs/:id", h.OCR.GetJobStatus)
|
protected.GET("/ocr/jobs/:id", h.OCR.GetJobStatus)
|
||||||
|
|
||||||
protected.GET("/files/:id/thumbnails", h.File.GetThumbnails)
|
protected.GET("/files/:id/thumbnails", h.File.GetThumbnails)
|
||||||
|
|
||||||
api.GET("/thumbnails/:id", h.File.ServeThumbnail)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -238,6 +238,48 @@ func (s *FileService) GetThumbnailStoragePath(id string) (string, error) {
|
|||||||
return t.StorageKey, nil
|
return t.StorageKey, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *FileService) GetBestThumbnail(fileID, preferredLabel string) *model.Thumbnail {
|
||||||
|
dbThumbnails, err := s.queries.GetThumbnailsByFileID(context.Background(), fileID)
|
||||||
|
if err != nil || len(dbThumbnails) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var fallback *model.Thumbnail
|
||||||
|
for _, t := range dbThumbnails {
|
||||||
|
if t.PageNumber != 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if t.ResolutionLabel == preferredLabel {
|
||||||
|
return &model.Thumbnail{
|
||||||
|
ID: t.ID,
|
||||||
|
FileID: t.FileID,
|
||||||
|
PageNumber: int(t.PageNumber),
|
||||||
|
ResolutionLabel: t.ResolutionLabel,
|
||||||
|
Width: int(t.Width),
|
||||||
|
Height: int(t.Height),
|
||||||
|
StorageKey: t.StorageKey,
|
||||||
|
MimeType: t.MimeType,
|
||||||
|
CreatedAt: t.CreatedAt.String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if fallback == nil {
|
||||||
|
fallback = &model.Thumbnail{
|
||||||
|
ID: t.ID,
|
||||||
|
FileID: t.FileID,
|
||||||
|
PageNumber: int(t.PageNumber),
|
||||||
|
ResolutionLabel: t.ResolutionLabel,
|
||||||
|
Width: int(t.Width),
|
||||||
|
Height: int(t.Height),
|
||||||
|
StorageKey: t.StorageKey,
|
||||||
|
MimeType: t.MimeType,
|
||||||
|
CreatedAt: t.CreatedAt.String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
func dbToModel(f db.File, dbTags []db.Tag) model.File {
|
func dbToModel(f db.File, dbTags []db.Tag) model.File {
|
||||||
tags := make([]model.Tag, len(dbTags))
|
tags := make([]model.Tag, len(dbTags))
|
||||||
for i, t := range dbTags {
|
for i, t := range dbTags {
|
||||||
|
|||||||
Reference in New Issue
Block a user