-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinclude-uploads.go
More file actions
332 lines (282 loc) · 9.3 KB
/
include-uploads.go
File metadata and controls
332 lines (282 loc) · 9.3 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
pageData := buildPageData("Add File", nil)
renderTemplate(w, "add.html", pageData)
return
}
// Parse the multipart form (with max memory limit, e.g., 32MB)
err := r.ParseMultipartForm(32 << 20)
if err != nil {
renderError(w, "Failed to parse form", http.StatusBadRequest)
return
}
files := r.MultipartForm.File["file"]
if len(files) == 0 {
renderError(w, "No files uploaded", http.StatusBadRequest)
return
}
var warnings []string
var lastID int64
// Process each file
for _, fileHeader := range files {
file, err := fileHeader.Open()
if err != nil {
renderError(w, "Failed to open uploaded file", http.StatusInternalServerError)
return
}
defer file.Close()
id, warningMsg, err := processUpload(file, fileHeader.Filename)
if err != nil {
renderError(w, err.Error(), http.StatusInternalServerError)
return
}
lastID = id
if warningMsg != "" {
warnings = append(warnings, warningMsg)
}
}
var warningMsg string
if len(warnings) > 0 {
warningMsg = strings.Join(warnings, "; ")
}
redirectTarget := "/untagged"
if len(files) == 1 && lastID != 0 {
redirectTarget = "/file/" + strconv.FormatInt(lastID, 10)
}
redirectWithWarning(w, r, redirectTarget, warningMsg)
}
func processUpload(src io.Reader, filename string) (int64, string, error) {
finalFilename, finalPath, conflictID, err := checkFileConflictStrict(filename)
if err != nil {
return 0, "", err
}
if conflictID != 0 {
return conflictID, "", nil
}
tempPath := finalPath + ".tmp"
tempFile, err := os.Create(tempPath)
if err != nil {
return 0, "", fmt.Errorf("failed to create temp file: %v", err)
}
_, err = io.Copy(tempFile, src)
tempFile.Close()
if err != nil {
os.Remove(tempPath)
return 0, "", fmt.Errorf("failed to copy file data: %v", err)
}
ext := strings.ToLower(filepath.Ext(filename))
videoExts := map[string]bool{
".mp4": true, ".mov": true, ".avi": true,
".mkv": true, ".webm": true, ".m4v": true,
}
var processedPath string
var warningMsg string
if videoExts[ext] || ext == ".cbz" {
// Process videos and CBZ files
processedPath, warningMsg, err = processVideoFile(tempPath, finalPath)
if err != nil {
os.Remove(tempPath)
return 0, "", err
}
} else {
// Non-video, non-CBZ → just rename temp file to final
if err := os.Rename(tempPath, finalPath); err != nil {
return 0, "", fmt.Errorf("failed to move file: %v", err)
}
processedPath = finalPath
}
id, err := saveFileToDatabase(finalFilename, processedPath)
if err != nil {
os.Remove(processedPath)
return 0, "", err
}
return id, warningMsg, nil
}
func uploadFromURLHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Redirect(w, r, "/upload", http.StatusSeeOther)
return
}
fileURL := r.FormValue("fileurl")
if fileURL == "" {
renderError(w, "No URL provided", http.StatusBadRequest)
return
}
customFilename := strings.TrimSpace(r.FormValue("filename"))
parsedURL, err := url.ParseRequestURI(fileURL)
if err != nil || !(parsedURL.Scheme == "http" || parsedURL.Scheme == "https") {
renderError(w, "Invalid URL", http.StatusBadRequest)
return
}
resp, err := http.Get(fileURL)
if err != nil || resp.StatusCode != http.StatusOK {
renderError(w, "Failed to download file", http.StatusBadRequest)
return
}
defer resp.Body.Close()
var filename string
urlExt := filepath.Ext(parsedURL.Path)
if customFilename != "" {
filename = customFilename
if filepath.Ext(filename) == "" && urlExt != "" {
filename += urlExt
}
} else {
parts := strings.Split(parsedURL.Path, "/")
filename = parts[len(parts)-1]
if filename == "" {
filename = "file_from_url"
}
}
id, warningMsg, err := processUpload(resp.Body, filename)
if err != nil {
renderError(w, err.Error(), http.StatusInternalServerError)
return
}
redirectWithWarning(w, r, fmt.Sprintf("/file/%d", id), warningMsg)
}
func ytdlpHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Redirect(w, r, "/upload", http.StatusSeeOther)
return
}
videoURL := r.FormValue("url")
if videoURL == "" {
renderError(w, "No URL provided", http.StatusBadRequest)
return
}
outTemplate := filepath.Join(config.UploadDir, "%(title)s.%(ext)s")
filenameCmd := exec.Command("yt-dlp", "--playlist-items", "1", "-f", "mp4", "-o", outTemplate, "--get-filename", videoURL)
filenameBytes, err := filenameCmd.Output()
if err != nil {
renderError(w, fmt.Sprintf("Failed to get filename: %v", err), http.StatusInternalServerError)
return
}
expectedFullPath := strings.TrimSpace(string(filenameBytes))
expectedFilename := filepath.Base(expectedFullPath)
finalFilename, finalPath, conflictID, err := checkFileConflictStrict(expectedFilename)
if err != nil {
renderError(w, err.Error(), http.StatusConflict)
return
}
if conflictID != 0 {
redirectWithWarning(w, r, fmt.Sprintf("/file/%d", conflictID), "")
return
}
downloadCmd := exec.Command("yt-dlp", "--playlist-items", "1", "-f", "mp4", "-o", outTemplate, videoURL)
downloadCmd.Stdout = os.Stdout
downloadCmd.Stderr = os.Stderr
if err := downloadCmd.Run(); err != nil {
renderError(w, fmt.Sprintf("Failed to download video: %v", err), http.StatusInternalServerError)
return
}
if expectedFullPath != finalPath {
if err := os.Rename(expectedFullPath, finalPath); err != nil {
renderError(w, fmt.Sprintf("Failed to move downloaded file: %v", err), http.StatusInternalServerError)
return
}
}
tempPath := finalPath + ".tmp"
if err := os.Rename(finalPath, tempPath); err != nil {
renderError(w, fmt.Sprintf("Failed to create temp file for processing: %v", err), http.StatusInternalServerError)
return
}
processedPath, warningMsg, err := processVideoFile(tempPath, finalPath)
if err != nil {
os.Remove(tempPath)
renderError(w, fmt.Sprintf("Failed to process video: %v", err), http.StatusInternalServerError)
return
}
id, err := saveFileToDatabase(finalFilename, processedPath)
if err != nil {
os.Remove(processedPath)
renderError(w, err.Error(), http.StatusInternalServerError)
return
}
redirectWithWarning(w, r, fmt.Sprintf("/file/%d", id), warningMsg)
}
func processVideoFile(tempPath, finalPath string) (string, string, error) {
ext := strings.ToLower(filepath.Ext(finalPath))
// Handle CBZ files
if ext == ".cbz" {
if err := os.Rename(tempPath, finalPath); err != nil {
return "", "", fmt.Errorf("failed to move file: %v", err)
}
if err := generateCBZThumbnail(finalPath, config.UploadDir, filepath.Base(finalPath)); err != nil {
log.Printf("Warning: could not generate CBZ thumbnail: %v", err)
}
return finalPath, "", nil
}
// Handle video files
codec, err := detectVideoCodec(tempPath)
if err != nil {
return "", "", err
}
if codec == "hevc" || codec == "h265" {
warningMsg := "The video uses HEVC and has been re-encoded to H.264 for browser compatibility."
if err := reencodeHEVCToH264(tempPath, finalPath); err != nil {
return "", "", fmt.Errorf("failed to re-encode HEVC video: %v", err)
}
os.Remove(tempPath)
if err := generateThumbnail(finalPath, config.UploadDir, filepath.Base(finalPath)); err != nil {
log.Printf("Warning: could not generate thumbnail after HEVC re-encode: %v", err)
}
return finalPath, warningMsg, nil
}
if err := os.Rename(tempPath, finalPath); err != nil {
return "", "", fmt.Errorf("failed to move file: %v", err)
}
// Generate thumbnail for video files
if ext == ".mp4" || ext == ".mov" || ext == ".avi" || ext == ".mkv" || ext == ".webm" || ext == ".m4v" {
if err := generateThumbnail(finalPath, config.UploadDir, filepath.Base(finalPath)); err != nil {
log.Printf("Warning: could not generate thumbnail: %v", err)
}
}
return finalPath, "", nil
}
func saveFileToDatabase(filename, path string) (int64, error) {
relPath, err := filepath.Rel(config.UploadDir, path)
if err != nil {
return 0, fmt.Errorf("failed to compute relative path: %v", err)
}
res, err := db.Exec("INSERT INTO files (filename, path, description) VALUES (?, ?, '')", filename, relPath)
if err != nil {
return 0, fmt.Errorf("failed to save file to database: %v", err)
}
id, err := res.LastInsertId()
if err != nil {
return 0, fmt.Errorf("failed to get inserted ID: %v", err)
}
computeProperties(id, path)
return id, nil
}
func detectVideoCodec(filePath string) (string, error) {
cmd := exec.Command("ffprobe", "-v", "error", "-select_streams", "v:0",
"-show_entries", "stream=codec_name", "-of", "default=nokey=1:noprint_wrappers=1", filePath)
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to probe video codec: %v", err)
}
return strings.TrimSpace(string(out)), nil
}
func reencodeHEVCToH264(inputPath, outputPath string) error {
cmd := exec.Command("ffmpeg", "-i", inputPath,
"-c:v", "libx264", "-profile:v", "baseline", "-preset", "fast", "-crf", "23",
"-c:a", "aac", "-movflags", "+faststart", outputPath)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
}