This commit is contained in:
Nabil Ould Hamou 2024-03-01 23:55:29 +01:00
parent a18674ce5f
commit 571f7e7c70
No known key found for this signature in database
GPG key ID: 2B1AE73E9B6014EF
2 changed files with 13 additions and 3 deletions

View file

@ -53,7 +53,12 @@ func CreatePost(c *gin.Context) {
if result.Error != nil { if result.Error != nil {
for _, f := range files { for _, f := range files {
filePath := filepath.Join("assets", f.FileName) var filePath string
if os.Getenv("GIN_MODE") == "release" {
filePath = filepath.Join(initializers.ReleaseBasePath, f.FileName)
} else {
filePath = filepath.Join(initializers.DebugBasePath, f.FileName)
}
if os.Remove(filePath) != nil { if os.Remove(filePath) != nil {
c.JSON(http.StatusInternalServerError, gin.H{ c.JSON(http.StatusInternalServerError, gin.H{
"error": "Internal server error", "error": "Internal server error",
@ -97,7 +102,12 @@ func uploadFiles(c *gin.Context, uploadedFiles []*multipart.FileHeader) ([]model
splitName := strings.Split(file.Filename, ".") splitName := strings.Split(file.Filename, ".")
newName := uuid.New().String() + "." + splitName[len(splitName)-1] newName := uuid.New().String() + "." + splitName[len(splitName)-1]
path := "assets/" + newName var path string
if os.Getenv("GIN_MODE") == "release" {
path = initializers.ReleaseBasePath + newName
} else {
path = initializers.DebugBasePath + newName
}
if err := c.SaveUploadedFile(file, path); err != nil { if err := c.SaveUploadedFile(file, path); err != nil {
return nil, err return nil, err

View file

@ -61,7 +61,7 @@ func GetUserById(c *gin.Context) {
} }
var user models.User var user models.User
result := initializers.DB.Preload("Posts").Preload("Files").First(&user, "id = ?", uniqueId) result := initializers.DB.Preload("Posts.Files").First(&user, "id = ?", uniqueId)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {