fix file creation

This commit is contained in:
divyam234 2023-12-03 04:33:16 +05:30
parent bc7b50f4da
commit ca77be389d
6 changed files with 28 additions and 24 deletions

View file

@ -24,7 +24,7 @@ RUN make pre-ui && make ui
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build \ RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build \
-ldflags='-w -s -extldflags "-static"' -a \ -ldflags='-w -s -extldflags "-static"' -a \
-o /app/teldrive . -o /app/teldrive cmd/teldrive/main.go
FROM --platform=${TARGETPLATFORM:-linux/amd64} busybox FROM --platform=${TARGETPLATFORM:-linux/amd64} busybox

View file

@ -4,7 +4,6 @@ import (
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"io" "io"
"os"
) )
func FromBytes(data []byte) string { func FromBytes(data []byte) string {
@ -17,16 +16,6 @@ func FromString(str string) string {
return FromBytes(data) 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) { func FromReader(src io.Reader) (string, error) {
h := md5.New() h := md5.New()
if _, err := io.Copy(h, src); err != nil { if _, err := io.Copy(h, src); err != nil {

View file

@ -5,21 +5,25 @@ import (
"github.com/divyam234/teldrive/pkg/schemas" "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{ return schemas.FileOut{
ID: file.ID, ID: file.ID,
Name: file.Name, Name: file.Name,
Type: file.Type, Type: file.Type,
MimeType: file.MimeType, MimeType: file.MimeType,
Path: file.Path, Path: file.Path,
Size: *file.Size, Size: size,
Starred: file.Starred, Starred: file.Starred,
ParentID: file.ParentID, ParentID: file.ParentID,
UpdatedAt: file.UpdatedAt, UpdatedAt: file.UpdatedAt,
} }
} }
func ToFileOutFull(file *models.File) *schemas.FileOutFull { func ToFileOutFull(file models.File) *schemas.FileOutFull {
parts := []schemas.Part{} parts := []schemas.Part{}
for _, part := range *file.Parts { for _, part := range *file.Parts {
parts = append(parts, schemas.Part{ parts = append(parts, schemas.Part{

View file

@ -65,7 +65,7 @@ type UpdateFile struct {
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
Path string `json:"path,omitempty"` Path string `json:"path,omitempty"`
Starred bool `json:"starred,omitempty"` Starred *bool `json:"starred,omitempty"`
ParentID string `json:"parentId,omitempty"` ParentID string `json:"parentId,omitempty"`
UpdatedAt time.Time `json:"updatedAt,omitempty"` UpdatedAt time.Time `json:"updatedAt,omitempty"`
} }

View file

@ -79,10 +79,21 @@ func (fs *FileService) CreateFile(c *gin.Context) (*schemas.FileOut, *types.AppE
} }
} }
fileDB.ChannelID = utils.Int64Pointer(channelId) 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.UserID = userId
fileDB.Starred = false
fileDB.Status = "active" fileDB.Status = "active"
if err := fs.Db.Create(&fileDB).Error; err != nil { 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 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} 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) 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 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) { 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} Code: http.StatusInternalServerError}
} }
file := mapper.ToFileOut(&files[0]) file := mapper.ToFileOut(files[0])
return &file, nil 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) fs.Db.Model(&models.File{}).Where("id = ?", payload.ID).Find(&res)
file := mapper.ToFileOutFull(&res[0]) file := mapper.ToFileOutFull(res[0])
newIds := models.Parts{} 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 return &out, nil