@@ -11,7 +11,6 @@ import (
11
11
"net/url"
12
12
"proofofaccess/ipfs"
13
13
"proofofaccess/localdata"
14
- "strconv"
15
14
"strings"
16
15
"time"
17
16
)
@@ -21,7 +20,6 @@ type APIResponse struct {
21
20
VideoV2 string `json:"video_v2"`
22
21
} `json:"recommended"`
23
22
}
24
-
25
23
type ProofMessage struct {
26
24
Status string `json:"Status"`
27
25
Message string `json:"Message"`
@@ -216,93 +214,87 @@ type CIDSize struct {
216
214
}
217
215
218
216
func PinVideos (gb int , ctx context.Context ) error {
219
- // Connect to the local IPFS node
217
+ fmt . Println ( "Pinning videos" )
220
218
sh := ipfs .Shell
221
-
222
- // Calculate total pinned storage
219
+ const GB = 1024 * 1024 * 1024
220
+ limit := int64 ( gb * GB )
223
221
totalPinned := int64 (0 )
224
- fmt . Println ( "Getting pins" )
225
- pins , err := sh . Pins ()
226
- if err != nil {
227
- fmt . Println ( "Error getting pins" )
228
- fmt . Println ( err )
222
+
223
+ // Convert ThreeSpeakVideos to a map for quicker lookups
224
+ videoMap := make ( map [ string ] bool )
225
+ for _ , cid := range localdata . ThreeSpeakVideos {
226
+ videoMap [ cid ] = true
229
227
}
230
- fmt .Println ("Got pins" )
231
228
232
- // Define the limit for the pinned storage
233
- const GB = 1024 * 1024 * 1024
234
- limit := int64 (gb * GB )
235
- fmt .Println ("Limit: " , strconv .FormatInt (limit , 10 ))
236
- // Generate list of CIDs with size
237
- cidList := make ([]CIDSize , len (localdata .ThreeSpeakVideos ))
238
- fmt .Println ("Making CID list" )
239
- fmt .Println ("Length of localdata.ThreeSpeakVideos: " )
240
- fmt .Println (len (localdata .ThreeSpeakVideos ))
241
-
242
- for i , cid := range localdata .ThreeSpeakVideos {
243
- select {
244
- case <- ctx .Done ():
245
- return ctx .Err ()
246
- default :
247
- if cid == "" {
248
- fmt .Printf ("Empty CID at index %d, skipping\n " , i )
249
- continue
250
- }
251
- fmt .Println ("CID: " + cid )
252
- stat , err := sh .ObjectStat (cid )
253
- fmt .Println ("Got object stats " , stat )
254
- if err != nil {
255
- fmt .Printf ("Failed to get object stats for CID %s: %v, skipping\n " , cid , err )
256
- continue
257
- }
229
+ // Check all the currently pinned CIDs
230
+ fmt .Println ("Checking currently pinned CIDs" )
231
+ allPinsData , _ := sh .Pins ()
258
232
259
- cidList [i ] = CIDSize {
260
- CID : cid ,
261
- Size : int64 (stat .CumulativeSize ),
262
- }
263
- totalPinned += int64 (stat .CumulativeSize )
264
- fmt .Println ("Total pinned: " + strconv .FormatInt (totalPinned , 10 ))
265
- }
233
+ // Map the allPins to only CIDs
234
+ allPins := make (map [string ]bool )
235
+ for cid := range allPinsData {
236
+ allPins [cid ] = true
266
237
}
267
238
268
- fmt .Println ("Got CID list" )
269
-
270
- // Pin new videos until limit is reached
271
- for _ , video := range cidList {
272
- select {
273
- case <- ctx .Done ():
274
- return ctx .Err ()
275
- default :
276
- if totalPinned + video .Size > limit {
277
- fmt .Println ("Total pinned storage exceeds limit" )
278
- break
279
- }
280
- fmt .Println ("Pinning CID: " + video .CID )
281
- if err := sh .Pin (video .CID ); err != nil {
282
- fmt .Println ("failed to pin CID %s: %w" , video .CID , err )
239
+ for cid , pinInfo := range allPinsData {
240
+ // Filter only the direct pins
241
+ if pinInfo .Type == "recursive" {
242
+ if videoMap [cid ] {
243
+ fmt .Println ("CID is in the video list" , cid )
244
+ // If the CID is in the video list, get its size and add to totalPinned
245
+ stat , err := sh .ObjectStat (cid )
246
+ if err != nil {
247
+ fmt .Printf ("Error getting stats for CID %s: %s\n " , cid , err )
248
+ continue
249
+ }
250
+ size := int64 (stat .CumulativeSize )
251
+ totalPinned += size
252
+ fmt .Println ("Total pinned: " , totalPinned )
253
+ } else {
254
+ // If the CID is not in the video list, unpin it
255
+ fmt .Println ("Unpinning CID: " , cid )
256
+ if err := sh .Unpin (cid ); err != nil {
257
+ fmt .Printf ("Error unpinning CID %s: %s\n " , cid , err )
258
+ continue
259
+ }
260
+ fmt .Println ("Unpinned CID: " , cid )
283
261
}
284
- fmt .Println ("Pinned CID: " + video .CID )
285
- totalPinned += video .Size
286
262
}
287
263
}
264
+ fmt .Println ("Total pinned: " , totalPinned )
288
265
289
- // Remove older videos if total pinned storage exceeds limit
290
- for cid := range pins {
291
- select {
292
- case <- ctx .Done ():
293
- return ctx .Err ()
294
- default :
266
+ // Pin videos from ThreeSpeak until the limit is reached
267
+ for _ , cid := range localdata .ThreeSpeakVideos {
268
+ if totalPinned >= limit {
269
+ fmt .Println ("Total pinned is greater than limit" )
270
+ break
271
+ }
272
+ fmt .Println ("Getting stats for CID: " , cid )
273
+ // If CID isn't already in allPins, then it's not pinned
274
+ if _ , pinned := allPins [cid ]; ! pinned {
295
275
stat , err := sh .ObjectStat (cid )
296
276
if err != nil {
297
- fmt .Println ("failed to get object stats for CID %s: %w" , cid , err )
298
- }
299
- if totalPinned <= limit {
300
- break
277
+ fmt .Printf ("Error getting stats for CID %s: %s\n " , cid , err )
278
+ continue
301
279
}
302
- if err := sh .Unpin (cid ); err != nil {
303
- fmt .Println ("failed to unpin CID %s: %w" , cid , err )
280
+
281
+ size := int64 (stat .CumulativeSize )
282
+ fmt .Println ("Size: " , size )
283
+ fmt .Println ("Total pinned: " , totalPinned )
284
+ fmt .Println ("Limit: " , limit )
285
+ if totalPinned + size - 1000000 <= limit {
286
+ fmt .Println ("Pinning CID: " , cid )
287
+ if err := sh .Pin (cid ); err != nil {
288
+ fmt .Printf ("Failed to pin CID %s: %s\n " , cid , err )
289
+ continue
290
+ }
291
+ totalPinned += size
292
+ // Once pinned, add it to the allPins
293
+ allPins [cid ] = true
294
+ fmt .Println ("Pinned CID: " , cid )
304
295
}
305
- totalPinned -= int64 (stat .CumulativeSize ) // Use actual size of the unpinned video
296
+ } else {
297
+ fmt .Println ("CID is already pinned" )
306
298
}
307
299
}
308
300
0 commit comments