-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload_media.go
60 lines (52 loc) · 1.43 KB
/
upload_media.go
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
package utils
import (
"github.com/Crossbell-Box/OperatorSync/app/worker/global"
"github.com/Crossbell-Box/OperatorSync/common/types"
"html"
"sync"
)
func UploadAllMedia(mediaUris []string) []types.Media {
// Collect all unique media URIs
mediaUriSet := make(map[string]struct{})
for _, rawMediaUri := range mediaUris {
if _, ok := mediaUriSet[rawMediaUri]; !ok {
mediaUriSet[rawMediaUri] = struct{}{}
}
}
// Upload them all
var ipfsUploadWg sync.WaitGroup
ipfsUploadResultChannel := make(chan types.Media, len(mediaUriSet))
for uri := range mediaUriSet {
innerUri := uri
ipfsUploadWg.Add(1)
go func() {
media, err := UploadOneMedia(innerUri)
if err != nil {
global.Logger.Error("Failed to upload link (", innerUri, ") onto IPFS: ", err.Error())
} else {
ipfsUploadResultChannel <- *media
}
ipfsUploadWg.Done()
}()
}
// Collect results
ipfsUploadWg.Wait()
close(ipfsUploadResultChannel)
var medias []types.Media
for media := range ipfsUploadResultChannel {
medias = append(medias, media)
}
return medias
}
func UploadOneMedia(mediaUri string) (*types.Media, error) {
unescapedUri := html.UnescapeString(mediaUri)
media := types.Media{
OriginalURI: unescapedUri,
}
var err error
if media.FileName, media.IPFSUri, media.FileSize, media.ContentType, media.AdditionalProps, err = UploadURLToIPFS(media.OriginalURI, false); err != nil {
return nil, err
} else {
return &media, nil
}
}