Skip to content

Commit

Permalink
refactor: optimize version storage and archive handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dongwlin committed Jan 8, 2025
1 parent 5b619a5 commit 44fd568
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions internal/logic/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ func (l *VersionLogic) Create(ctx context.Context, param CreateVersionParam) (*e
return l.createRollback(tx, err)
}
storageRootDir := filepath.Join(cwd, "storage")
saveDir := filepath.Join(storageRootDir, strconv.Itoa(param.ResourceID), strconv.Itoa(v.ID), "resource")
versionDir := filepath.Join(storageRootDir, strconv.Itoa(param.ResourceID), strconv.Itoa(v.ID))
saveDir := filepath.Join(versionDir, "resource")
if err := os.MkdirAll(saveDir, os.ModePerm); err != nil {
l.logger.Error("Failed to create storage directory",
zap.Error(err),
Expand Down Expand Up @@ -126,11 +127,34 @@ func (l *VersionLogic) Create(ctx context.Context, param CreateVersionParam) (*e
return l.createRollbackRemoveSaveDir(tx, err, saveDir)
}

if err := os.Remove(param.UploadArchivePath); err != nil {
l.logger.Error("Failed to remove temp file",
zap.Error(err),
)
return l.createRollbackRemoveSaveDir(tx, err, saveDir)
archivePath := filepath.Join(versionDir, "resource.zip")
if strings.HasSuffix(param.UploadArchivePath, ".zip") {
err = os.Rename(param.UploadArchivePath, archivePath)
if err != nil {
l.logger.Error("Failed to move archive file",
zap.String("origin path", param.UploadArchivePath),
zap.String("destination path", archivePath),
zap.Error(err),
)
return l.createRollbackRemoveSaveDir(tx, err, saveDir)
}
} else {
if err := os.Remove(param.UploadArchivePath); err != nil {
l.logger.Error("Failed to remove temp file",
zap.Error(err),
)
return l.createRollbackRemoveSaveDir(tx, err, saveDir)
}
err = archive.CompressToZip(saveDir, archivePath)
if err != nil {
l.logger.Error("Failed to compress to zip",
zap.String("src dir", saveDir),
zap.String("dst file", archivePath),
zap.Error(err),
)
return l.createRollbackRemoveSaveDir(tx, err, saveDir)
}

}

fileHashes, err := filehash.GetAll(saveDir)
Expand Down

0 comments on commit 44fd568

Please sign in to comment.