write meta

This commit is contained in:
celogeek 2022-03-04 17:40:04 +01:00
parent 5621e59e42
commit c602d69f90
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
2 changed files with 21 additions and 6 deletions

View File

@ -113,7 +113,9 @@ func (s *Service) FileCreateChunk(c *gin.Context) {
io.Copy(b, c.Request.Body)
c.Request.Body.Close()
if err := s.Store.NewChunk(b.Bytes()).Save(); err != nil {
sess := s.CurrentSession(c)
if err := s.Store.NewChunk(b.Bytes()).Save(sess); err != nil {
if errors.Is(err, photoserrors.ErrStoreChunkAlreadyExists) {
s.Error(c, http.StatusOK, err)
} else {

View File

@ -3,11 +3,15 @@ package store
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/gin-gonic/gin"
"gitlab.celogeek.com/photos/api/internal/photos/models"
"gitlab.celogeek.com/photos/api/internal/photoserrors"
)
@ -62,7 +66,7 @@ func (c *Chunk) Mkdir() error {
return os.MkdirAll(c.Dir(), 0755)
}
func (c *Chunk) Save() error {
func (c *Chunk) Save(sess *models.Session) error {
if c.FileExists() {
return photoserrors.ErrStoreChunkAlreadyExists
}
@ -71,13 +75,22 @@ func (c *Chunk) Save() error {
return err
}
fs, err := os.Create(c.Filename())
if err := os.WriteFile(c.Filename(), c.Bytes, 0666); err != nil {
return err
}
meta, err := os.Create(c.Filename() + ".json")
if err != nil {
return err
}
defer fs.Close()
_, err = fs.Write(c.Bytes)
return err
enc := json.NewEncoder(meta)
enc.SetIndent("", " ")
return enc.Encode(gin.H{
"author": sess.Account.Login,
"date": time.Now().UTC().Format("2006-01-02 15:04:05"),
"checksum": c.Sum,
"size": len(c.Bytes),
})
}
func (s *Store) Combine(sumb []string) (string, uint64, error) {