package files import ( "context" "errors" "io" "time" "git.aiterp.net/rpdata/api/internal/store" "git.aiterp.net/rpdata/api/models" ) // Upload adds a file to the space. func Upload(ctx context.Context, name, mimeType, author string, size int64, input io.Reader) (models.File, error) { if !allowdMimeTypes[mimeType] { return models.File{}, errors.New("File type not allowed:" + mimeType) } if name == "" { date := time.Now().UTC().Format("Jan 02 2006 15:04:05 MST") name = "Unnamed file (" + date + ")" } if mimeType == "" { mimeType = "binary/octet-stream" } id := makeID() path, err := store.UploadFile(ctx, "files", id, mimeType, input, size) if err != nil { return models.File{}, err } file := models.File{ ID: id, Kind: "upload", Time: time.Now(), Public: false, Author: author, Name: name, MimeType: mimeType, Size: size, URL: store.URLFromPath(path), } err = collection.Insert(file) if err != nil { return models.File{}, err } return file, nil }