-
Notifications
You must be signed in to change notification settings - Fork 1
/
media.go
173 lines (146 loc) · 3.87 KB
/
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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
)
var mediaDataDir = directoryFromStruct(media{}, false)
var mediaFilesDir = directoryFromStruct(media{}, true)
type mediaList []media
var mediaData mediaList
type media struct {
ID int `json:"id"`
Name string `json:"name"`
MediaType string `json:"media_type"`
MediaDetails mediaDetails `json:"media_details"`
PostID int `json:"post"`
Slug string `json:"slug"`
}
type mediaDetails struct {
Sizes sizes `json:"sizes"`
}
type sizes struct {
Full full `json:"full"`
}
type full struct {
FileName string `json:"file"`
Width int `json:"width"`
Height int `json:"height"`
SourceURL string `json:"source_url"`
}
func mediaDataFromFiles() error {
// get a list of all the files in the dir
fileList, err := ioutil.ReadDir(mediaDataDir)
//fmt.Println(mediaDataDir)
if err != nil {
return err
}
// iterate over the files and read them
for _, f := range fileList {
//fmt.Println(filepath.Join(mediaDataDir, f.Name()))
// Open the json file
jsonFile, err := os.Open(filepath.Join(mediaDataDir, f.Name()))
// handle error
if err != nil {
return err
}
defer jsonFile.Close()
// read our opened json file as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
var tmpmd mediaList
// unmarshall the json into byte array
err = json.Unmarshal(byteValue, &tmpmd)
if err != nil {
return err
}
// append the tag data
mediaData = append(mediaData, tmpmd...)
}
return nil
}
// mediaNameFromID - gets the media name from the passed in ID
func mediaNameFromID(mID int, titleSlug string) (string, error) {
if len(mediaData) == 0 {
err := mediaDataFromFiles()
if err != nil {
return "", err
}
}
for i := range mediaData {
if mediaData[i].ID == mID {
// change the filename so that it's more obvious which post it belongs to
fileExt := filepath.Ext(mediaData[i].MediaDetails.Sizes.Full.FileName)
changedFilename := fmt.Sprintf("%s%s", titleSlug, fileExt)
downloadedFile, err := mediaFileFromURL(mediaData[i].MediaDetails.Sizes.Full.SourceURL, changedFilename)
if err != nil {
return "", err
}
return downloadedFile, nil
}
}
return "", nil
}
// mediaFileFromURL - access the data from the API for local store / processing
// URL - URL to the file for downloading
// f - the filename to use when storing the file
func mediaFileFromURL(URL string, f string) (string, error) {
fileToDownload := filepath.Join(mediaFilesDir, f)
// check the file exists and don't get again
if _, err := os.Stat(fileToDownload); os.IsNotExist(err) {
// check output dir exists
if _, err := os.Stat(mediaFilesDir); os.IsNotExist(err) {
os.MkdirAll(mediaFilesDir, os.ModePerm)
}
// get the data
response, err := http.Get(URL)
if err != nil {
return "", err
}
defer response.Body.Close()
// create the file
file, err := os.Create(fileToDownload)
if err != nil {
return "", err
}
defer file.Close()
// write to the file
_, err = io.Copy(file, response.Body)
if err != nil {
return "", err
}
}
fmt.Fprintf(os.Stdout, "File downloaded : %s\n", fileToDownload)
return fileToDownload, nil
}
func fileExtFromFile(file string) (string, error) {
f, err := os.Open(file)
if err != nil {
return "", err
}
defer f.Close()
ct, err := fileContentTypeFromFile(f)
if err != nil {
return "", err
}
fExt, err := imageFileExtFromMimeType(ct)
if err != nil {
return "", err
}
//fmt.Println("File Extension :", ext)
return fExt, nil
}
// Read the first 512 bytes to detect the content type of the file
func fileContentTypeFromFile(out *os.File) (string, error) {
buffer := make([]byte, 512)
_, err := out.Read(buffer)
if err != nil {
return "", err
}
defer out.Close()
contentType := http.DetectContentType(buffer)
return contentType, nil
}