From ca77be389da6aa42fbc3cd336478779b6b6197e6 Mon Sep 17 00:00:00 2001 From: divyam234 <47589864+divyam234@users.noreply.github.com> Date: Sun, 3 Dec 2023 04:33:16 +0530 Subject: [PATCH] fix file creation --- Dockerfile | 2 +- internal/md5/md5.go | 11 -------- pkg/mapper/mapper.go | 10 ++++--- .../{authenticateJWT.go => authjwt.go} | 0 pkg/schemas/file.go | 2 +- pkg/services/file.go | 27 +++++++++++++------ 6 files changed, 28 insertions(+), 24 deletions(-) rename pkg/middleware/{authenticateJWT.go => authjwt.go} (100%) diff --git a/Dockerfile b/Dockerfile index e2a7501..c9b1d90 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,7 +24,7 @@ RUN make pre-ui && make ui RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build \ -ldflags='-w -s -extldflags "-static"' -a \ - -o /app/teldrive . + -o /app/teldrive cmd/teldrive/main.go FROM --platform=${TARGETPLATFORM:-linux/amd64} busybox diff --git a/internal/md5/md5.go b/internal/md5/md5.go index 06fc123..140f456 100644 --- a/internal/md5/md5.go +++ b/internal/md5/md5.go @@ -4,7 +4,6 @@ import ( "crypto/md5" "fmt" "io" - "os" ) func FromBytes(data []byte) string { @@ -17,16 +16,6 @@ func FromString(str string) string { return FromBytes(data) } -func FromFilePath(filePath string) (string, error) { - f, err := os.Open(filePath) - if err != nil { - return "", err - } - defer f.Close() - - return FromReader(f) -} - func FromReader(src io.Reader) (string, error) { h := md5.New() if _, err := io.Copy(h, src); err != nil { diff --git a/pkg/mapper/mapper.go b/pkg/mapper/mapper.go index 7732c5f..21c5a11 100644 --- a/pkg/mapper/mapper.go +++ b/pkg/mapper/mapper.go @@ -5,21 +5,25 @@ import ( "github.com/divyam234/teldrive/pkg/schemas" ) -func ToFileOut(file *models.File) schemas.FileOut { +func ToFileOut(file models.File) schemas.FileOut { + var size int64 + if file.Size != nil { + size = *file.Size + } return schemas.FileOut{ ID: file.ID, Name: file.Name, Type: file.Type, MimeType: file.MimeType, Path: file.Path, - Size: *file.Size, + Size: size, Starred: file.Starred, ParentID: file.ParentID, UpdatedAt: file.UpdatedAt, } } -func ToFileOutFull(file *models.File) *schemas.FileOutFull { +func ToFileOutFull(file models.File) *schemas.FileOutFull { parts := []schemas.Part{} for _, part := range *file.Parts { parts = append(parts, schemas.Part{ diff --git a/pkg/middleware/authenticateJWT.go b/pkg/middleware/authjwt.go similarity index 100% rename from pkg/middleware/authenticateJWT.go rename to pkg/middleware/authjwt.go diff --git a/pkg/schemas/file.go b/pkg/schemas/file.go index 6401e32..c7098b9 100644 --- a/pkg/schemas/file.go +++ b/pkg/schemas/file.go @@ -65,7 +65,7 @@ type UpdateFile struct { Name string `json:"name,omitempty"` Type string `json:"type,omitempty"` Path string `json:"path,omitempty"` - Starred bool `json:"starred,omitempty"` + Starred *bool `json:"starred,omitempty"` ParentID string `json:"parentId,omitempty"` UpdatedAt time.Time `json:"updatedAt,omitempty"` } diff --git a/pkg/services/file.go b/pkg/services/file.go index c958537..f48bf11 100644 --- a/pkg/services/file.go +++ b/pkg/services/file.go @@ -79,10 +79,21 @@ func (fs *FileService) CreateFile(c *gin.Context) (*schemas.FileOut, *types.AppE } } fileDB.ChannelID = utils.Int64Pointer(channelId) - } + fileDB.MimeType = fileIn.MimeType + parts := models.Parts{} + for _, part := range fileIn.Parts { + parts = append(parts, models.Part{ + ID: part.ID, + }) + } + fileDB.Parts = &parts + fileDB.Starred = false + fileDB.Size = &fileIn.Size + } + fileDB.Name = fileIn.Name + fileDB.Type = fileIn.Type fileDB.UserID = userId - fileDB.Starred = false fileDB.Status = "active" if err := fs.Db.Create(&fileDB).Error; err != nil { @@ -94,7 +105,7 @@ func (fs *FileService) CreateFile(c *gin.Context) (*schemas.FileOut, *types.AppE } - res := mapper.ToFileOut(&fileDB) + res := mapper.ToFileOut(fileDB) return &res, nil } @@ -125,7 +136,7 @@ func (fs *FileService) UpdateFile(c *gin.Context) (*schemas.FileOut, *types.AppE return nil, &types.AppError{Error: errors.New("file not updated"), Code: http.StatusInternalServerError} } - file := mapper.ToFileOut(&files[0]) + file := mapper.ToFileOut(files[0]) key := fmt.Sprintf("files:%s", fileID) @@ -147,7 +158,7 @@ func (fs *FileService) GetFileByID(c *gin.Context) (*schemas.FileOutFull, error) return nil, errors.New("file not found") } - return mapper.ToFileOutFull(&file[0]), nil + return mapper.ToFileOutFull(file[0]), nil } func (fs *FileService) ListFiles(c *gin.Context) (*schemas.FileResponse, *types.AppError) { @@ -278,7 +289,7 @@ func (fs *FileService) MakeDirectory(c *gin.Context) (*schemas.FileOut, *types.A Code: http.StatusInternalServerError} } - file := mapper.ToFileOut(&files[0]) + file := mapper.ToFileOut(files[0]) return &file, nil @@ -300,7 +311,7 @@ func (fs *FileService) CopyFile(c *gin.Context) (*schemas.FileOut, *types.AppErr fs.Db.Model(&models.File{}).Where("id = ?", payload.ID).Find(&res) - file := mapper.ToFileOutFull(&res[0]) + file := mapper.ToFileOutFull(res[0]) newIds := models.Parts{} @@ -381,7 +392,7 @@ func (fs *FileService) CopyFile(c *gin.Context) (*schemas.FileOut, *types.AppErr } - out := mapper.ToFileOut(&dbFile) + out := mapper.ToFileOut(dbFile) return &out, nil