feat: detect and add mime type to upload so we don't need to make extra requests on runtime

This commit is contained in:
Derrick Hammer 2024-01-25 19:05:52 -05:00
parent 00a58a3b98
commit f3be950ba7
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 29 additions and 3 deletions

View File

@ -19,7 +19,7 @@ type StorageService interface {
GetHashSmall(file io.ReadSeeker) ([]byte, error) GetHashSmall(file io.ReadSeeker) ([]byte, error)
GetHash(file io.Reader) ([]byte, int64, error) GetHash(file io.Reader) ([]byte, int64, error)
GetFile(hash []byte, start int64) (io.ReadCloser, int64, error) GetFile(hash []byte, start int64) (io.ReadCloser, int64, error)
CreateUpload(hash []byte, uploaderID uint, uploaderIP string, size uint64, protocol string) (*models.Upload, error) CreateUpload(hash []byte, mime string, uploaderID uint, uploaderIP string, size uint64, protocol string) (*models.Upload, error)
TusUploadExists(hash []byte) (bool, models.TusUpload) TusUploadExists(hash []byte) (bool, models.TusUpload)
CreateTusUpload(hash []byte, uploadID string, uploaderID uint, uploaderIP string, protocol string) (*models.TusUpload, error) CreateTusUpload(hash []byte, uploadID string, uploaderID uint, uploaderIP string, protocol string) (*models.TusUpload, error)
TusUploadProgress(uploadID string) error TusUploadProgress(uploadID string) error

View File

@ -26,6 +26,7 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
"io" "io"
"lukechampine.com/blake3" "lukechampine.com/blake3"
"net/http"
"net/url" "net/url"
"strings" "strings"
"time" "time"
@ -280,11 +281,12 @@ func (s *StorageServiceImpl) GetHash(file io.Reader) ([]byte, int64, error) {
return hash[:32], totalBytes, nil return hash[:32], totalBytes, nil
} }
func (s *StorageServiceImpl) CreateUpload(hash []byte, uploaderID uint, uploaderIP string, size uint64, protocol string) (*models.Upload, error) { func (s *StorageServiceImpl) CreateUpload(hash []byte, mime string, uploaderID uint, uploaderIP string, size uint64, protocol string) (*models.Upload, error) {
hashStr := hex.EncodeToString(hash) hashStr := hex.EncodeToString(hash)
upload := &models.Upload{ upload := &models.Upload{
Hash: hashStr, Hash: hashStr,
MimeType: mime,
UserID: uploaderID, UserID: uploaderID,
UploaderIP: uploaderIP, UploaderIP: uploaderIP,
Protocol: protocol, Protocol: protocol,
@ -522,6 +524,30 @@ func (s *StorageServiceImpl) buildNewTusUploadTask(upload *models.TusUpload) (jo
return err return err
} }
var mimeBuf [512]byte
_, err = reader.Read(mimeBuf[:])
if err != nil {
s.portal.Logger().Error("Could not read mime", zap.Error(err))
return err
}
mimeType := http.DetectContentType(mimeBuf[:])
upload.MimeType = mimeType
if tx := s.Portal().Database().Save(upload); tx.Error != nil {
s.portal.Logger().Error("Could not update tus upload", zap.Error(tx.Error))
return tx.Error
}
reader, err = tusUpload.GetReader(ctx)
if err != nil {
s.portal.Logger().Error("Could not get tus file", zap.Error(err))
return err
}
err = s.PutFile(reader, upload.Protocol, dbHash) err = s.PutFile(reader, upload.Protocol, dbHash)
if err != nil { if err != nil {
@ -551,7 +577,7 @@ func (s *StorageServiceImpl) buildNewTusUploadTask(upload *models.TusUpload) (jo
return err return err
} }
newUpload, err := s.CreateUpload(dbHash, upload.UploaderID, upload.UploaderIP, uint64(byteCount), upload.Protocol) newUpload, err := s.CreateUpload(dbHash, mimeType, upload.UploaderID, upload.UploaderIP, uint64(byteCount), upload.Protocol)
if err != nil { if err != nil {
s.portal.Logger().Error("Could not create upload", zap.Error(err)) s.portal.Logger().Error("Could not create upload", zap.Error(err))
return err return err