package services import ( "context" "git.aiterp.net/stufflog/server/internal/slerrors" "git.aiterp.net/stufflog/server/internal/space" "io" "strings" ) type Upload struct { s3 *space.Space } func (upload *Upload) UploadImage(ctx context.Context, name string, contentType string, size int64, reader io.Reader) (string, error) { if !allowedImages[contentType] { return "", slerrors.Forbidden("Content type " + contentType + " is not allowed as an image.") } if !strings.HasSuffix(name, extensions[contentType]) { name += extensions[contentType] } err := upload.s3.UploadFile(ctx, name, contentType, reader, size) if err != nil { return "", err } return upload.s3.URLFromPath(name), nil } func (upload *Upload) Delete(ctx context.Context, url string) error { name, err := upload.s3.PathFromURL(url) if err != nil { return err } return upload.s3.RemoveFile(ctx, name) } var allowedImages = map[string]bool{ "image/jpg": true, "image/jpeg": true, "image/gif": true, "image/png": true, "image/bmp": false, } var extensions = map[string]string{ "image/jpg": ".jpg", "image/jpeg": ".jpg", "image/gif": ".gif", "image/png": ".png", }