@Summary Get a file share record @Description Get share record by share_id and verify permission before proxying to file content. @Tags FileShare @Accept json @Produce json @Security BearerAuth @Param share_id path string true "Share ID" @Success 200
(ctx *gin.Context)
| 86 | // @Success 200 {object} model.CommonResponse{data=model.File} |
| 87 | // @Router /api/file-shares/{share_id} [get] |
| 88 | func (c *FileShareController) GetFileShare(ctx *gin.Context) { |
| 89 | shareID := ctx.Param("share_id") |
| 90 | if shareID == "" { |
| 91 | ctx.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("missing share_id")) |
| 92 | return |
| 93 | } |
| 94 | eid := config.GetEID(ctx) |
| 95 | userID := config.GetUserId(ctx) |
| 96 | |
| 97 | rec, err := c.service.GetShareRecord(ctx, shareID) |
| 98 | if err != nil { |
| 99 | switch err { |
| 100 | case sharefiles.ErrNotFound: |
| 101 | ctx.JSON(http.StatusNotFound, gin.H{"error": "not found"}) |
| 102 | case sharefiles.ErrExpired: |
| 103 | ctx.JSON(http.StatusNotFound, gin.H{"error": "not found"}) |
| 104 | default: |
| 105 | logger.Errorf(ctx, "GetFileShare query error: %v", err) |
| 106 | ctx.JSON(http.StatusInternalServerError, gin.H{"error": "internal error"}) |
| 107 | } |
| 108 | return |
| 109 | } |
| 110 | // 参数类型适配:controller 层为 int64,sharefiles 需要 uint64 |
| 111 | // 负值保护,避免无效调用 |
| 112 | if eid < 0 || userID < 0 || rec.FileID < 0 { |
| 113 | ctx.JSON(http.StatusForbidden, gin.H{"error": "forbidden"}) |
| 114 | return |
| 115 | } |
| 116 | // 复用 GetFile 的响应:设置 :id 参数后调用 |
| 117 | ctx.Params = append(ctx.Params, gin.Param{Key: "file_id", Value: itoaFS(rec.FileID)}) |
| 118 | // 获取当前权限 |
| 119 | maxPermission, err := service.GetUserPermission(eid, model.RESOURCE_TYPE_FILE, rec.FileID, userID) |
| 120 | if err != nil { |
| 121 | maxPermission = model.PERMISSION_NONE |
| 122 | } |
| 123 | if maxPermission < model.PERMISSION_VIEW_ONLY { |
| 124 | // 如果权限小于仅查看,才需要增加查看权限 |
| 125 | _ = service.UpsertPermission(eid, model.RESOURCE_TYPE_FILE, rec.FileID, model.SUBJECT_TYPE_USER, userID, model.PERMISSION_VIEW_ONLY) |
| 126 | } |
| 127 | GetFile(ctx) |
| 128 | } |
| 129 | |
| 130 | // itoa 复用 controller/share.go 的实现 |
| 131 | func itoaFS(v int64) string { |
nothing calls this directly
no test coverage detected