-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheif.go
215 lines (189 loc) · 5.72 KB
/
heif.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//https://github.com/golang-samples/http/blob/master/fileupload/main.go
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"encoding/json"
"strings"
"path/filepath"
"time"
"bytes"
"github.com/satori/go.uuid"
"github.com/gorilla/mux"
)
// 100 MB
const MAX_MEMORY = 100 * 1024 * 1024
const extension string = "heic"
type HeifConfig struct {
General General `json:"general"`
Content []Content `json:"content"`
}
type General struct {
Output Output `json:"output"`
Brands Brands `json:"brands"`
}
type Output struct {
FilePath string `json:"file_path"`
}
type Brands struct {
Major string `json:"major"`
Other []string `json:"other"`
}
type Content struct {
Master Master `json:"master"`
Thumbs []Thumbs `json:"thumbs"`
}
type Master struct {
FilePath string `json:"file_path"`
HdlrType string `json:"hdlr_type"`
CodeType string `json:"code_type"`
EncpType string `json:"encp_type"`
}
type Thumbs struct {
FilePath string `json:"file_path"`
HdlrType string `json:"hdlr_type"`
CodeType string `json:"code_type"`
EncpType string `json:"encp_type"`
SyncRate int `json:"sync_rate"`
}
func download(w http.ResponseWriter, r *http.Request) {
// http://www.giantflyingsaucer.com/blog/?p=5635
vars := mux.Vars(r)
files := vars["files"]
guid := vars["uuid"]
name := vars["name"]
if (files != "" && name != "" && guid != "") {
heif_file_name := "./" + files + "/" + guid + "/" + name
log.Println(heif_file_name)
// https://stackoverflow.com/a/29024834/319826
heif_file, err := ioutil.ReadFile(heif_file_name)
if err != nil {
log.Println("Unable to read HEIF-file")
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w,"No file found!")
} else {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment; filename=" +heif_file_name)
w.Header().Set("Content-Transfer-Encoding", "binary")
w.Header().Set("Expires", "0")
w.Header().Set("Status", "OK")
http.ServeContent(w, r, heif_file_name, time.Now(), bytes.NewReader(heif_file))
}
}
}
func upload(w http.ResponseWriter, r *http.Request) {
guid := uuid.NewV4()
var UPLOAD_FOLDER string = "files/" + guid.String()
var ffmpeg_cmd = "/usr/local/bin/ffmpeg"
var heif_cmd = "/usr/local/bin/writerapp"
var args = []string{}
var main_file = UPLOAD_FOLDER + "/bitstream.265"
var thumb_file = UPLOAD_FOLDER + "/bitstream.thumb.265"
var configfile = UPLOAD_FOLDER + "/config.json"
if _, err := os.Stat(UPLOAD_FOLDER); os.IsNotExist(err) {
if create_folder_err := os.Mkdir(UPLOAD_FOLDER, 0755); create_folder_err != nil {
log.Println("Unable to create upload folder '" + UPLOAD_FOLDER + "'.")
return
} else {
log.Println("Upload folder '" + UPLOAD_FOLDER + "' created.")
}
}
if err := r.ParseMultipartForm(MAX_MEMORY); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusForbidden)
}
for key, value := range r.MultipartForm.Value {
fmt.Fprintf(w, "%s:%s", key, value)
log.Printf("%s:%s", key, value)
}
for _, fileHeaders := range r.MultipartForm.File {
for _, fileHeader := range fileHeaders {
filename := fileHeader.Filename
file, _ := fileHeader.Open()
path := fmt.Sprintf(UPLOAD_FOLDER + "/%s", filename)
buf, _ := ioutil.ReadAll(file)
ioutil.WriteFile(path, buf, 0644)
heif_file_name := UPLOAD_FOLDER + "/" + strings.TrimSuffix(filename, filepath.Ext(filename)) + "." + extension
log.Println("File '" + path + "' saved.")
log.Println("New filename: " + heif_file_name)
// Create json
// https://stackoverflow.com/a/24356483/319826
heif_config := &HeifConfig {
General: General {
Output: Output {
FilePath: heif_file_name,
},
Brands: Brands {
Major: "mif1",
Other: []string{"mif1", "heic", "hevc"},
},
},
Content: []Content {
{Master: Master {
FilePath: main_file,
HdlrType: "pict",
CodeType: "hvc1",
EncpType: "meta",
},
Thumbs: []Thumbs {
{
FilePath: thumb_file,
HdlrType: "pict",
CodeType: "hvc1",
EncpType: "meta",
SyncRate: 1,
},
},
},
},
}
// End create json
b, err := json.Marshal(heif_config)
if err != nil {
log.Println(err)
} else {
fileErr := ioutil.WriteFile(configfile, b, 0644)
if fileErr != nil {
log.Println(fileErr)
return
}
}
pwd, err := exec.Command("pwd").Output()
if err != nil {
log.Println(pwd)
}
args = []string{"-y", "-i", path, "-crf", "12", "-preset", "slower", "-pix_fmt", "yuv420p", "-f", "hevc", main_file}
if err := exec.Command(ffmpeg_cmd, args...).Run(); err != nil {
log.Println("Unable to create main file!")
return
}
args = []string{"-y", "-i", path, "-vf", "scale=320:240", "-crf", "28", "-preset", "slower", "-pix_fmt", "yuv420p", "-f", "hevc", thumb_file}
if err := exec.Command(ffmpeg_cmd, args...).Run(); err != nil {
log.Println("Unable to create thumbnail!")
return
}
//build_heif_file = "/usr/local/bin/writerapp " + UPLOAD_FOLDER + "/" + configfile
args = []string{configfile}
if err = exec.Command(heif_cmd, args...).Run(); err != nil {
log.Println("Unable to encode to HEIF-format!")
log.Println(err)
return
}
log.Println("File '" + path + "' converted to HEIF-format!")
// https://stackoverflow.com/a/35934496/319826
log.Println("/download/" + heif_file_name)
http.Redirect(w, r, "/download/" + heif_file_name, http.StatusSeeOther)
}
}
}
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/download/{files}/{uuid}/{name}", download).Methods("GET")
router.HandleFunc("/upload", upload).Methods("POST")
http.Handle("/", http.FileServer(http.Dir("static")))
log.Fatal(http.ListenAndServe(":8080", router))
}