Have got saving working
This commit is contained in:
parent
f9a65c8ca9
commit
c8a276b248
21 changed files with 248 additions and 22 deletions
|
|
@ -6,7 +6,10 @@ import (
|
|||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
)
|
||||
|
|
@ -67,6 +70,75 @@ func (s *Service) renderCopyTemplate(upload models.Upload) string {
|
|||
return sb.String()
|
||||
}
|
||||
|
||||
func (s *Service) ReplaceUploadFile(ctx context.Context, uploadID int64, srcPath string) (models.Upload, error) {
|
||||
site, _, err := s.fetchSiteAndUser(ctx)
|
||||
if err != nil {
|
||||
return models.Upload{}, err
|
||||
}
|
||||
|
||||
upload, err := s.db.SelectUploadByID(ctx, uploadID)
|
||||
if err != nil {
|
||||
return models.Upload{}, err
|
||||
} else if upload.SiteID != site.ID {
|
||||
return models.Upload{}, models.NotFoundError
|
||||
}
|
||||
|
||||
if err := s.up.ReplaceFile(site, upload, srcPath); err != nil {
|
||||
return models.Upload{}, err
|
||||
}
|
||||
|
||||
stat, err := os.Stat(srcPath)
|
||||
if err != nil {
|
||||
return models.Upload{}, err
|
||||
}
|
||||
upload.FileSize = stat.Size()
|
||||
|
||||
if err := s.db.UpdateUploadFileSize(ctx, upload.ID, upload.FileSize); err != nil {
|
||||
return models.Upload{}, err
|
||||
}
|
||||
|
||||
return upload, nil
|
||||
}
|
||||
|
||||
func (s *Service) CreateUploadFromFile(ctx context.Context, srcPath string, filename string, mimeType string) (models.Upload, error) {
|
||||
site, _, err := s.fetchSiteAndUser(ctx)
|
||||
if err != nil {
|
||||
return models.Upload{}, err
|
||||
}
|
||||
|
||||
stat, err := os.Stat(srcPath)
|
||||
if err != nil {
|
||||
return models.Upload{}, err
|
||||
}
|
||||
|
||||
newUploadGUID := models.NewNanoID()
|
||||
newTime := time.Now().UTC()
|
||||
newSlug := filepath.Join(
|
||||
fmt.Sprintf("%04d", newTime.Year()),
|
||||
fmt.Sprintf("%02d", newTime.Month()),
|
||||
newUploadGUID+filepath.Ext(filename),
|
||||
)
|
||||
|
||||
newUpload := models.Upload{
|
||||
SiteID: site.ID,
|
||||
GUID: models.NewNanoID(),
|
||||
FileSize: stat.Size(),
|
||||
MIMEType: mimeType,
|
||||
Filename: filename,
|
||||
CreatedAt: newTime,
|
||||
Slug: newSlug,
|
||||
}
|
||||
if err := s.db.SaveUpload(ctx, &newUpload); err != nil {
|
||||
return models.Upload{}, err
|
||||
}
|
||||
|
||||
if err := s.up.AdoptFile(site, newUpload, srcPath); err != nil {
|
||||
return models.Upload{}, err
|
||||
}
|
||||
|
||||
return newUpload, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListUploads(ctx context.Context) (res []UploadWithURL, _ error) {
|
||||
site, _, err := s.fetchSiteAndUser(ctx)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue