🚚 Move io related functions to fs internal package

This commit is contained in:
LittleSheep 2024-12-28 14:07:53 +08:00
parent 99dd7f55e0
commit dda85eae98
7 changed files with 26 additions and 18 deletions

View File

@ -0,0 +1,3 @@
# File System
The reason of why this package exists is because "cycle import was not allowed"

View File

@ -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 {

View File

@ -1,4 +1,4 @@
package services
package fs
import (
"context"

View File

@ -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)

View File

@ -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.")
}

View File

@ -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

View File

@ -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)