feat: add initial version of GetFile

This commit is contained in:
Derrick Hammer 2024-01-24 01:26:40 -05:00
parent fb1112f3a2
commit 12093637ed
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 46 additions and 0 deletions

View File

@ -18,6 +18,7 @@ type StorageService interface {
FileExists(hash []byte) (bool, models.Upload) FileExists(hash []byte) (bool, models.Upload)
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) (io.ReadCloser, uint64, error)
CreateUpload(hash []byte, uploaderID uint, uploaderIP string, size uint64, protocol string) (*models.Upload, error) CreateUpload(hash []byte, 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)

View File

@ -596,3 +596,48 @@ func splitS3Ids(id string) (objectId, multipartId string) {
multipartId = id[index+1:] multipartId = id[index+1:]
return return
} }
func (s *StorageServiceImpl) GetFile(hash []byte) (io.ReadCloser, uint64, error) {
if exists, tusUpload := s.TusUploadExists(hash); exists {
if tusUpload.Completed {
upload, err := s.tusStore.GetUpload(context.Background(), tusUpload.UploadID)
if err != nil {
return nil, 0, err
}
info, _ := upload.GetInfo(context.Background())
reader, err := upload.GetReader(context.Background())
return reader, uint64(info.Size), err
}
}
exists, upload := s.FileExists(hash)
if !exists {
return nil, 0, errors.New("file does not exist")
}
hashStr := hex.EncodeToString(hash)
resp, err := s.httpApi.R().
SetPathParam("path", hashStr).
DisableAutoReadResponse().
Get("/api/bus/object/{path}")
if err != nil {
return nil, 0, err
}
if resp.IsError() {
if resp.Error() != nil {
return nil, 0, resp.Error().(error)
}
return nil, 0, errors.New(resp.String())
}
return resp.Body, upload.Size, nil
}