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) io.Copy(b, c.Request.Body)
c.Request.Body.Close() 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) { if errors.Is(err, photoserrors.ErrStoreChunkAlreadyExists) {
s.Error(c, http.StatusOK, err) s.Error(c, http.StatusOK, err)
} else { } else {

View File

@ -3,11 +3,15 @@ package store
import ( import (
"crypto/sha1" "crypto/sha1"
"encoding/hex" "encoding/hex"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"time"
"github.com/gin-gonic/gin"
"gitlab.celogeek.com/photos/api/internal/photos/models"
"gitlab.celogeek.com/photos/api/internal/photoserrors" "gitlab.celogeek.com/photos/api/internal/photoserrors"
) )
@ -62,7 +66,7 @@ func (c *Chunk) Mkdir() error {
return os.MkdirAll(c.Dir(), 0755) return os.MkdirAll(c.Dir(), 0755)
} }
func (c *Chunk) Save() error { func (c *Chunk) Save(sess *models.Session) error {
if c.FileExists() { if c.FileExists() {
return photoserrors.ErrStoreChunkAlreadyExists return photoserrors.ErrStoreChunkAlreadyExists
} }
@ -71,13 +75,22 @@ func (c *Chunk) Save() error {
return err 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 { if err != nil {
return err return err
} }
defer fs.Close() enc := json.NewEncoder(meta)
_, err = fs.Write(c.Bytes) enc.SetIndent("", " ")
return err 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) { func (s *Store) Combine(sumb []string) (string, uint64, error) {