-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdelete.go
More file actions
93 lines (74 loc) · 1.98 KB
/
delete.go
File metadata and controls
93 lines (74 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package task
import (
"fmt"
"time"
"github.com/golang/glog"
"github.com/livepeer/go-api-client"
"github.com/livepeer/livepeer-data/pkg/data"
"golang.org/x/sync/errgroup"
)
func TaskDelete(tctx *TaskContext) (*TaskHandlerOutput, error) {
var (
ctx = tctx.Context
asset = tctx.InputAsset
osSess = tctx.outputOS
)
directory := asset.PlaybackID
files, err := osSess.ListFiles(ctx, directory, "/")
if err != nil {
glog.Errorf("Error listing files in directory %v", directory)
}
totalFiles := files.Files()
accumulator := NewAccumulator()
tctx.Progress.TrackCount(accumulator.Size, uint64(len(totalFiles)), 0)
const maxRetries = 3
const retryInterval = time.Second
eg := errgroup.Group{}
for _, file := range totalFiles {
filename := file.Name
eg.Go(func() error {
var err error
for i := 0; i < maxRetries; i++ {
err = osSess.DeleteFile(ctx, filename)
if err == nil {
accumulator.Accumulate(1)
return nil
}
glog.Errorf("Error deleting file %v: %v (retrying...)", filename, err)
time.Sleep(retryInterval)
}
return fmt.Errorf("failed to delete file %v after %d retries", filename, maxRetries)
})
}
if err := eg.Wait(); err != nil {
glog.Errorf("Error deleting files: %v", err)
}
if ipfs := asset.AssetSpec.Storage.IPFS; ipfs != nil {
err = UnpinFromIpfs(*tctx, ipfs.CID, "cid")
if err != nil {
glog.Errorf("Error unpinning from IPFS %v", ipfs.CID)
}
err = UnpinFromIpfs(*tctx, ipfs.NFTMetadata.CID, "nftMetadataCid")
if err != nil {
glog.Errorf("Error unpinning metadata from IPFS %v", ipfs.NFTMetadata.CID)
}
}
return &TaskHandlerOutput{
TaskOutput: &data.TaskOutput{},
}, nil
}
func UnpinFromIpfs(tctx TaskContext, cid string, filter string) error {
assets, _, err := tctx.lapi.ListAssets(api.ListOptions{
Filters: map[string]interface{}{
filter: cid,
},
AllUsers: true,
})
if err != nil {
return err
}
if len(assets) == 1 {
return tctx.ipfs.Unpin(tctx.Context, cid)
}
return nil
}