Started working on the view upload page
This commit is contained in:
parent
0a9af9cde8
commit
48f39133d7
18 changed files with 327 additions and 45 deletions
75
services/uploads/manage.go
Normal file
75
services/uploads/manage.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
package uploads
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
)
|
||||
|
||||
type UploadWithURL struct {
|
||||
Upload models.Upload
|
||||
URL string
|
||||
}
|
||||
|
||||
func (s *Service) FetchUpload(ctx context.Context, uploadID int64) (res UploadWithURL, _ error) {
|
||||
site, _, err := s.fetchSiteAndUser(ctx)
|
||||
if err != nil {
|
||||
return UploadWithURL{}, err
|
||||
}
|
||||
|
||||
upload, err := s.db.SelectUploadByID(ctx, uploadID)
|
||||
if err != nil {
|
||||
return UploadWithURL{}, err
|
||||
}
|
||||
|
||||
return UploadWithURL{
|
||||
Upload: upload,
|
||||
URL: fmt.Sprintf("/sites/%v/uploads/%v/raw", site.ID, upload.ID),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListUploads(ctx context.Context) (res []UploadWithURL, _ error) {
|
||||
site, _, err := s.fetchSiteAndUser(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
uploads, err := s.db.SelectUploadsOfSite(ctx, site.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res = make([]UploadWithURL, len(uploads))
|
||||
for i, upload := range uploads {
|
||||
res[i] = UploadWithURL{
|
||||
Upload: upload,
|
||||
URL: fmt.Sprintf("/sites/%v/uploads/%v/raw", site.ID, upload.ID),
|
||||
}
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *Service) OpenUpload(ctx context.Context, id int64) (models.Upload, func() (io.ReadCloser, error), error) {
|
||||
site, _, err := s.fetchSiteAndUser(ctx)
|
||||
if err != nil {
|
||||
return models.Upload{}, nil, err
|
||||
}
|
||||
|
||||
upload, err := s.db.SelectUploadByID(ctx, id)
|
||||
if err != nil {
|
||||
return models.Upload{}, nil, err
|
||||
} else if upload.SiteID != site.ID {
|
||||
return models.Upload{}, nil, models.NotFoundError
|
||||
}
|
||||
|
||||
return upload, func() (io.ReadCloser, error) {
|
||||
rw, err := s.up.OpenUpload(site, upload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rw, nil
|
||||
}, nil
|
||||
}
|
||||
|
|
@ -5,7 +5,9 @@ import (
|
|||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
|
@ -100,13 +102,22 @@ func (s *Service) FinalizePending(ctx context.Context, pendingGUID string, expec
|
|||
return err
|
||||
}
|
||||
|
||||
newUploadGUID := models.NewNanoID()
|
||||
newTime := time.Now().UTC()
|
||||
newSlug := filepath.Join(
|
||||
fmt.Sprintf("%04d", newTime.Year()),
|
||||
fmt.Sprintf("%02d", newTime.Month()),
|
||||
newUploadGUID+filepath.Ext(pu.Filename),
|
||||
)
|
||||
|
||||
newUpload := models.Upload{
|
||||
SiteID: site.ID,
|
||||
GUID: models.NewNanoID(),
|
||||
FileSize: pu.FileSize,
|
||||
MIMEType: pu.MIMEType,
|
||||
Filename: pu.Filename,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
CreatedAt: newTime,
|
||||
Slug: newSlug,
|
||||
}
|
||||
if err := s.db.SaveUpload(ctx, &newUpload); err != nil {
|
||||
return err
|
||||
|
|
@ -115,6 +126,12 @@ func (s *Service) FinalizePending(ctx context.Context, pendingGUID string, expec
|
|||
if err := s.up.AdoptFile(site, newUpload, pendingDataFilename); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.db.DeletePendingUpload(ctx, newUpload.GUID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.up.StripeEXIFData(site, newUpload); err != nil {
|
||||
log.Printf("warn: failed to extract exif data from %s: %v\n", newUpload.Slug, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue