| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package services
- import (
- "context"
- "fmt"
- "time"
- "fwh-toolbox-server/cache"
- minioRepo "fwh-toolbox-server/repositories/minio"
- mongoRepo "fwh-toolbox-server/repositories/mongo"
- pgsqlRepo "fwh-toolbox-server/repositories/pgsql"
- )
- func DeleteDataDownloadDocument(ctx context.Context, documentID string, force bool) error {
- references, err := mongoRepo.CountDataDownloadDocumentReferences(ctx, documentID)
- if err != nil {
- return err
- }
- if references > 0 && !force {
- return fmt.Errorf("document is referenced by %d task(s); use force=true to delete", references)
- }
- requestLock, ok, err := cache.TryLock(ctx, dataDownloadRequestKey(documentID), 5*time.Minute)
- if err != nil {
- return err
- }
- if !ok {
- return fmt.Errorf("document has an active file request")
- }
- defer requestLock.SafeRelease(context.Background())
- lock, err := cache.Lock(ctx, "data-download:content:"+documentID, 5*time.Minute, 250*time.Millisecond, 5*time.Second)
- if err != nil {
- return fmt.Errorf("document has an active reader or writer: %w", err)
- }
- defer lock.SafeRelease(context.Background())
- document, err := mongoRepo.GetDataDownloadDocument(ctx, documentID)
- if err != nil {
- return err
- }
- artifacts, err := mongoRepo.ListDataDownloadArtifacts(ctx, documentID)
- if err != nil {
- return err
- }
- entries, err := mongoRepo.ListDataDownloadFaultRecordEntries(ctx, documentID)
- if err != nil {
- return err
- }
- if document.BinaryObjectKey != "" {
- if err := minioRepo.Delete(ctx, document.BinaryObjectKey); err != nil {
- return err
- }
- }
- for i := range artifacts {
- if err := minioRepo.Delete(ctx, artifacts[i].ObjectKey); err != nil {
- return err
- }
- }
- for i := range entries {
- if err := minioRepo.Delete(ctx, entries[i].ObjectKey); err != nil {
- return err
- }
- }
- if err := pgsqlRepo.DeleteDataDownloadContent(documentID); err != nil {
- return err
- }
- return mongoRepo.DeleteDataDownloadDocumentMetadata(ctx, documentID)
- }
|