add protection against overriding files

This commit is contained in:
Jonas Leder 2025-01-25 23:34:25 +01:00
commit 3eeb79fa7a
2 changed files with 24 additions and 2 deletions

View file

@ -70,3 +70,9 @@ func Download(c context.Context, filename string) ([]byte, string, error) {
return fileContent, objectMetadata.ContentType, nil
}
func CheckIfExist(c context.Context, filename string) bool {
_, err := MinioClient.StatObject(c, Bucket, filename, minio.GetObjectOptions{})
return err == nil
}

View file

@ -51,13 +51,29 @@ func initUpload() {
return
}
fileUUID := uuid.NewV4()
objectName := fileUUID.String() + "." + fileExtension
var objectName string
retries := 0
for {
fileUUID := uuid.NewV4()
objectName = fileUUID.String() + "." + fileExtension
if !s3.CheckIfExist(c, objectName) {
break
}
log.Log.Info("generated double UUID: ", fileUUID)
if retries >= 10 {
log.Log.Error("Failed finding any valid UUID, something is very broken")
c.JSON(http.StatusInternalServerError, gin.H{"message": "failed uploading file", "key": objectName})
return
}
retries++
}
_, err = s3.Upload(c, objectName, body, contentType)
if err != nil {
log.Log.Error("Failed uploading file to S3: ", err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"message": "failed uploading file", "key": objectName})
return
}
domain := c.Request.Host