delete route
This commit is contained in:
parent
8942778377
commit
064ce7979d
1 changed files with 51 additions and 0 deletions
|
@ -62,6 +62,57 @@ func RegisterFileRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
||||||
c.JSON(200, gin.H{"message": "file uploaded successfully", "file_id": fileMeta.ID})
|
c.JSON(200, gin.H{"message": "file uploaded successfully", "file_id": fileMeta.ID})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
api.DELETE("/delete", auth.JwtMiddleware(cfg.JWTSecret), func(c *gin.Context) {
|
||||||
|
claims := c.MustGet("claims").(jwt.MapClaims)
|
||||||
|
user := claims["user"].(auth.User)
|
||||||
|
|
||||||
|
uid := user.ID
|
||||||
|
if uid == "" {
|
||||||
|
c.JSON(401, gin.H{"error": "unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var response struct {
|
||||||
|
FileID string `json:"file_id" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.ShouldBindJSON(&response); err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": "file_id is required"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resfID := response.FileID
|
||||||
|
if resfID == "" {
|
||||||
|
c.JSON(400, gin.H{"error": "file_id cannot be empty"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.SplitN(resfID, "_", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
c.JSON(400, gin.H{"error": "invalid file_id format"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fileID, filename := parts[0], parts[1]
|
||||||
|
if fileID != uid {
|
||||||
|
c.JSON(403, gin.H{"error": "you can only delete your own files"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
filePath := filepath.Join(cfg.ImagePath, uid, filename)
|
||||||
|
if err := os.Remove(filePath); err != nil {
|
||||||
|
c.JSON(500, gin.H{"error": "failed to delete file"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := cfg.Database.Where("id = ?", resfID).Delete(&types.File{}).Error; err != nil {
|
||||||
|
c.JSON(500, gin.H{"error": "failed to delete file metadata"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, gin.H{"message": "file deleted successfully"})
|
||||||
|
})
|
||||||
|
|
||||||
api.GET("/:name", func(c *gin.Context) {
|
api.GET("/:name", func(c *gin.Context) {
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
parts := strings.SplitN(name, "_", 2)
|
parts := strings.SplitN(name, "_", 2)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue