From dda85eae988c8b3cc78a41a0f77b873c3f825629 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 28 Dec 2024 14:07:53 +0800 Subject: [PATCH] :truck: Move io related functions to fs internal package --- pkg/internal/fs/README.md | 3 +++ pkg/internal/{services => fs}/merger.go | 10 +--------- pkg/internal/{services => fs}/recycler.go | 2 +- pkg/internal/server/api/up_multipart_api.go | 14 ++++++++++++-- pkg/internal/services/analyzer.go | 3 ++- pkg/internal/services/attachments.go | 3 ++- pkg/main.go | 9 +++++---- 7 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 pkg/internal/fs/README.md rename pkg/internal/{services => fs}/merger.go (89%) rename pkg/internal/{services => fs}/recycler.go (99%) diff --git a/pkg/internal/fs/README.md b/pkg/internal/fs/README.md new file mode 100644 index 0000000..53c55d5 --- /dev/null +++ b/pkg/internal/fs/README.md @@ -0,0 +1,3 @@ +# File System + +The reason of why this package exists is because "cycle import was not allowed" diff --git a/pkg/internal/services/merger.go b/pkg/internal/fs/merger.go similarity index 89% rename from pkg/internal/services/merger.go rename to pkg/internal/fs/merger.go index ec83dc0..3285b46 100644 --- a/pkg/internal/services/merger.go +++ b/pkg/internal/fs/merger.go @@ -1,4 +1,4 @@ -package services +package fs import ( "fmt" @@ -56,14 +56,6 @@ func MergeFileChunks(meta models.AttachmentFragment, arrange []string) (models.A } } - // Post-upload tasks - if err := database.C.Save(&attachment).Error; err != nil { - return attachment, err - } - - CacheAttachment(attachment) - PublishAnalyzeTask(attachment) - // Clean up: remove chunk files go DeleteFragment(meta) for _, chunk := range arrange { diff --git a/pkg/internal/services/recycler.go b/pkg/internal/fs/recycler.go similarity index 99% rename from pkg/internal/services/recycler.go rename to pkg/internal/fs/recycler.go index 37e3750..c041f96 100644 --- a/pkg/internal/services/recycler.go +++ b/pkg/internal/fs/recycler.go @@ -1,4 +1,4 @@ -package services +package fs import ( "context" diff --git a/pkg/internal/server/api/up_multipart_api.go b/pkg/internal/server/api/up_multipart_api.go index 69b0f98..e7ccfeb 100644 --- a/pkg/internal/server/api/up_multipart_api.go +++ b/pkg/internal/server/api/up_multipart_api.go @@ -6,6 +6,7 @@ import ( "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/fs" "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" "git.solsynth.dev/hypernet/paperclip/pkg/internal/server/exts" "git.solsynth.dev/hypernet/paperclip/pkg/internal/services" @@ -115,10 +116,19 @@ func uploadFragmentChunk(c *fiber.Ctx) error { return c.JSON(meta) } - attachment, err := services.MergeFileChunks(meta, chunkArrange) + // Merge & post-upload + attachment, err := fs.MergeFileChunks(meta, chunkArrange) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) - } else if !c.QueryBool("analyzeNow", false) { + } + + // Post-upload tasks + if err := database.C.Save(&attachment).Error; err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + services.CacheAttachment(attachment) + + if !c.QueryBool("analyzeNow", false) { services.AnalyzeAttachment(attachment) } else { services.PublishAnalyzeTask(attachment) diff --git a/pkg/internal/services/analyzer.go b/pkg/internal/services/analyzer.go index 67de4d7..f15b500 100644 --- a/pkg/internal/services/analyzer.go +++ b/pkg/internal/services/analyzer.go @@ -17,6 +17,7 @@ import ( "github.com/samber/lo" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/fs" "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" jsoniter "github.com/json-iterator/go" "github.com/k0kubun/go-ansi" @@ -245,7 +246,7 @@ func AnalyzeAttachment(file models.Attachment) error { } else { // Recycle the temporary file file.Destination = models.AttachmentDstTemporary - go DeleteFile(file) + go fs.DeleteFile(file) // Finish log.Info().Dur("elapsed", time.Since(start)).Uint("id", file.ID).Msg("A file post-analyze upload task was finished.") } diff --git a/pkg/internal/services/attachments.go b/pkg/internal/services/attachments.go index b4fecd7..b8f5a62 100644 --- a/pkg/internal/services/attachments.go +++ b/pkg/internal/services/attachments.go @@ -17,6 +17,7 @@ import ( localCache "git.solsynth.dev/hypernet/paperclip/pkg/internal/cache" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/fs" "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" "github.com/google/uuid" @@ -218,7 +219,7 @@ func DeleteAttachment(item models.Attachment) error { tx.Commit() if dat.RefCount == 0 { - go DeleteFile(dat) + go fs.DeleteFile(dat) } return nil diff --git a/pkg/main.go b/pkg/main.go index a0aa434..a859823 100644 --- a/pkg/main.go +++ b/pkg/main.go @@ -8,6 +8,7 @@ import ( "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" pkg "git.solsynth.dev/hypernet/paperclip/pkg/internal" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/fs" "git.solsynth.dev/hypernet/paperclip/pkg/internal/gap" "github.com/fatih/color" @@ -80,9 +81,9 @@ func main() { // Configure timed tasks quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger))) quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup) - quartz.AddFunc("@every 60m", services.RunMarkLifecycleDeletionTask) - quartz.AddFunc("@every 60m", services.RunMarkMultipartDeletionTask) - quartz.AddFunc("@midnight", services.RunScheduleDeletionTask) + quartz.AddFunc("@every 60m", fs.RunMarkLifecycleDeletionTask) + quartz.AddFunc("@every 60m", fs.RunMarkMultipartDeletionTask) + quartz.AddFunc("@midnight", fs.RunScheduleDeletionTask) quartz.Start() // Server @@ -93,7 +94,7 @@ func main() { // Post-boot actions services.ScanUnanalyzedFileFromDatabase() - services.RunMarkLifecycleDeletionTask() + fs.RunMarkLifecycleDeletionTask() // Messages quit := make(chan os.Signal, 1)