Skip to content

Commit

Permalink
fix(api): create snapshot on overwrite (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba authored Jan 23, 2025
1 parent 8172131 commit cc66948
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 42 deletions.
17 changes: 17 additions & 0 deletions api/helper/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package helper

import "strings"

func PathFromFilename(name string) []string {
var components []string
for _, component := range strings.Split(name, "/") {
if component != "" {
components = append(components, component)
}
}
return components
}

func FilenameFromPath(path []string) string {
return path[len(path)-1]
}
4 changes: 2 additions & 2 deletions api/repo/file_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func newFileRepo() *fileRepo {
type FileInsertOptions struct {
Name string
WorkspaceID string
ParentID *string
ParentID string
Type string
}

Expand All @@ -214,7 +214,7 @@ func (repo *fileRepo) Insert(opts FileInsertOptions) (model.File, error) {
WorkspaceID: opts.WorkspaceID,
Name: opts.Name,
Type: opts.Type,
ParentID: opts.ParentID,
ParentID: &opts.ParentID,
}
if db := repo.db.Create(&file); db.Error != nil {
return nil, db.Error
Expand Down
6 changes: 3 additions & 3 deletions api/router/file_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (r *FileRouter) Create(c *fiber.Ctx) error {
file, err := r.fileSvc.Create(service.FileCreateOptions{
Name: name,
Type: model.FileTypeFile,
ParentID: &parentID,
ParentID: parentID,
WorkspaceID: workspaceID,
}, userID)
if err != nil {
Expand Down Expand Up @@ -177,7 +177,7 @@ func (r *FileRouter) Create(c *fiber.Ctx) error {
res, err := r.fileSvc.Create(service.FileCreateOptions{
Name: name,
Type: model.FileTypeFolder,
ParentID: &parentID,
ParentID: parentID,
WorkspaceID: workspaceID,
}, userID)
if err != nil {
Expand Down Expand Up @@ -1125,7 +1125,7 @@ func (r *FileRouter) CreateFromS3(c *fiber.Ctx) error {
file, err := r.fileSvc.Create(service.FileCreateOptions{
Name: name,
Type: model.FileTypeFile,
ParentID: &parentID,
ParentID: parentID,
WorkspaceID: workspaceID,
}, userID)
if err != nil {
Expand Down
80 changes: 43 additions & 37 deletions api/service/file_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ func NewFileService() *FileService {
}

type FileCreateOptions struct {
WorkspaceID string `json:"workspaceId" validate:"required"`
Name string `json:"name" validate:"required,max=255"`
Type string `json:"type" validate:"required,oneof=file folder"`
ParentID *string `json:"parentId" validate:"required"`
WorkspaceID string `json:"workspaceId" validate:"required"`
Name string `json:"name" validate:"required,max=255"`
Type string `json:"type" validate:"required,oneof=file folder"`
ParentID string `json:"parentId" validate:"required"`
}

type File struct {
Expand All @@ -114,56 +114,62 @@ type File struct {
}

func (svc *FileService) Create(opts FileCreateOptions, userID string) (*File, error) {
var components []string
for _, component := range strings.Split(opts.Name, "/") {
if component != "" {
components = append(components, component)
}
}
path := helper.PathFromFilename(opts.Name)
parentID := opts.ParentID
if len(components) > 1 {
for _, component := range components[:len(components)-1] {
existing, err := svc.getChildWithName(*parentID, component)
if err != nil {
return nil, err
}
if existing != nil {
parentID = new(string)
*parentID = existing.GetID()
} else {
res, err := svc.create(FileCreateOptions{
Name: component,
Type: model.FileTypeFolder,
ParentID: parentID,
WorkspaceID: opts.WorkspaceID,
}, userID)
if err != nil {
return nil, err
}
parentID = &res.ID
}
if len(path) > 1 {
newParentID, err := svc.createDirectoriesForPath(path, parentID, opts.WorkspaceID, userID)
if err != nil {
return nil, err
}
parentID = *newParentID
}
name := components[len(components)-1]
return svc.create(FileCreateOptions{
WorkspaceID: opts.WorkspaceID,
Name: name,
Name: helper.FilenameFromPath(path),
Type: opts.Type,
ParentID: parentID,
}, userID)
}

func (svc *FileService) createDirectoriesForPath(path []string, parentID string, workspaceID string, userID string) (*string, error) {
for _, component := range path[:len(path)-1] {
existing, err := svc.getChildWithName(parentID, component)
if err != nil {
return nil, err
}
if existing != nil {
parentID = existing.GetID()
} else {
folder, err := svc.create(FileCreateOptions{
Name: component,
Type: model.FileTypeFolder,
ParentID: parentID,
WorkspaceID: workspaceID,
}, userID)
if err != nil {
return nil, err
}
parentID = folder.ID
}
}
return &parentID, nil
}

func (svc *FileService) create(opts FileCreateOptions, userID string) (*File, error) {
if len(*opts.ParentID) > 0 {
if err := svc.validateParent(*opts.ParentID, userID); err != nil {
if len(opts.ParentID) > 0 {
if err := svc.validateParent(opts.ParentID, userID); err != nil {
return nil, err
}
existing, err := svc.getChildWithName(*opts.ParentID, opts.Name)
existing, err := svc.getChildWithName(opts.ParentID, opts.Name)
if err != nil {
return nil, err
}
if existing != nil {
return nil, errorpkg.NewFileWithSimilarNameExistsError()
res, err := svc.fileMapper.mapOne(existing, userID)
if err != nil {
return nil, err
}
return res, nil
}
}
file, err := svc.fileRepo.Insert(repo.FileInsertOptions{
Expand Down

0 comments on commit cc66948

Please sign in to comment.