This repository was archived by the owner on May 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostPublicationsHelperURI.go
More file actions
76 lines (64 loc) · 1.6 KB
/
Copy pathpostPublicationsHelperURI.go
File metadata and controls
76 lines (64 loc) · 1.6 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
package main
import (
"io"
"net/http"
"net/url"
"os"
"path/filepath"
)
// Decode attachments and place them in the temporary directory
func (pubreq *postPublicationsRequest) downloadURIAttachments() (pictures []string, videos []string, audio []string, documents []string, err error) {
if len(pubreq.PicturesURI) != 0 {
pictures, err = downloadURIAttachments("PIC", pubreq.PicturesURI)
if err != nil {
return
}
}
if len(pubreq.VideosURI) != 0 {
videos, err = downloadURIAttachments("VID", pubreq.VideosURI)
if err != nil {
return
}
}
if len(pubreq.AudioURI) != 0 {
audio, err = downloadURIAttachments("AUD", pubreq.AudioURI)
if err != nil {
return
}
}
if len(pubreq.DocumentsURI) != 0 {
documents, err = downloadURIAttachments("DOC", pubreq.DocumentsURI)
}
return
}
// Download a particular attachment type
func downloadURIAttachments(prefix string, attachmentsURI []string) ([]string, error) {
var paths []string = make([]string, len(attachmentsURI))
for index, uriStr := range attachmentsURI {
// Download file
resp, err := http.Get(uriStr)
if err != nil {
return paths, err
}
defer resp.Body.Close()
// Transform the uri into a net/url form
url, err := url.Parse(uriStr)
if err != nil {
return paths, err
}
// Build filename
filename := filepath.Base(url.Path)
path := filepath.Join(tempDirPath, filename)
// Create file
file, err := os.Create(path)
if err != nil {
return paths, err
}
defer file.Close()
// Make buffer, decode & write to file
io.Copy(file, resp.Body)
// Detect file type
paths[index] = path
}
return paths, nil
}