add space and finish item create/update mutations.
This commit is contained in:
+11
-4
@@ -1,19 +1,26 @@
|
||||
package services
|
||||
|
||||
import "git.aiterp.net/stufflog/server/database"
|
||||
import (
|
||||
"git.aiterp.net/stufflog/server/database"
|
||||
"git.aiterp.net/stufflog/server/internal/space"
|
||||
)
|
||||
|
||||
type Bundle struct {
|
||||
Auth *Auth
|
||||
Auth *Auth
|
||||
Upload *Upload
|
||||
}
|
||||
|
||||
func NewBundle(db database.Database) Bundle {
|
||||
func NewBundle(db database.Database, s3 *space.Space) Bundle {
|
||||
auth := &Auth{
|
||||
users: db.Users(),
|
||||
session: db.Session(),
|
||||
projects: db.Projects(),
|
||||
}
|
||||
|
||||
upload := &Upload{s3: s3}
|
||||
|
||||
return Bundle{
|
||||
Auth: auth,
|
||||
Auth: auth,
|
||||
Upload: upload,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
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",
|
||||
}
|
||||
Reference in New Issue
Block a user