-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploaded_media.go
158 lines (127 loc) · 3.81 KB
/
uploaded_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
package main
import (
"context"
"io"
"log"
"net"
"net/http"
"os"
"path"
"time"
"github.com/dhowden/tag"
)
// UploadedMediaProvider is an audio source provider that handles uploaded media files.
type UploadedMediaProvider struct {
cachePath string
uploadServerURL string
}
// NewUploadedMediaProvider creates a new UploadedMediaProvider instance.
func NewUploadedMediaProvider(cachePath string) *UploadedMediaProvider {
u, err := startUploadedMediaServer(cachePath)
if err != nil {
log.Fatalf("failed to start upload server: %s", err)
}
return &UploadedMediaProvider{
cachePath: cachePath,
uploadServerURL: u,
}
}
// Name returns the name of the provider.
func (*UploadedMediaProvider) Name() string {
return "User media"
}
// HandleRequest handles an HTTP request and returns an audio source.
func (p *UploadedMediaProvider) HandleRequest(w http.ResponseWriter, req *http.Request) audioSource {
fd, header, err := req.FormFile("media")
if err != nil {
log.Printf("failed to read uploaded file: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return nil
}
tmpPath := path.Join(p.cachePath, header.Filename)
tmpFd, err := os.Create(tmpPath)
if err != nil {
log.Printf("failed to create temp file %s: %s", tmpPath, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return nil
}
defer tmpFd.Close()
if _, err := io.Copy(tmpFd, fd); err != nil {
log.Printf("failed to copy uploaded file to %s: %s", tmpPath, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return nil
}
if err := tmpFd.Sync(); err != nil {
log.Printf("failed to store uploaded file to %s: %s", tmpPath, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return nil
}
log.Printf("stored uploaded file to %s", tmpPath)
http.Redirect(w, req, req.Referer(), http.StatusSeeOther)
meta := UploadedMedia{
FileName: header.Filename,
Title: header.Filename,
MIMEType: header.Header.Get("Content-Type"),
downloadURL: p.uploadServerURL + "/" + header.Filename,
}
tmpFd.Seek(0, 0)
if m, err := tag.ReadFrom(tmpFd); err == nil {
if a := m.Artist(); a != "" {
meta.Author = a
} else if a = m.AlbumArtist(); a != "" {
meta.Author = a
}
if t := m.Title(); t != "" {
meta.Title = m.Title()
}
if t := m.Album(); t != "" {
meta.Title = t + ": " + meta.Title
}
} else {
log.Printf("failed to read uploaded file metadata: %s", err)
}
return meta
}
// UploadedMedia is an audio source that represents an uploaded media file.
type UploadedMedia struct {
Author string
Title string
Duration time.Duration
FileName string
MIMEType string
downloadURL string
}
// Metadata returns the metadata of the audio source.
func (m UploadedMedia) Metadata(ctx context.Context) (Metadata, error) {
return Metadata{
Type: UploadedItem,
Author: m.Author,
Title: m.Title,
Description: m.Title,
Duration: m.Duration,
MIMEType: m.MIMEType,
}, nil
}
// DownloadURL returns the download URL of the audio source.
func (m UploadedMedia) DownloadURL(ctx context.Context) (string, error) {
return m.downloadURL, nil
}
func startUploadedMediaServer(storagePath string) (string, error) {
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
return "", err
}
h := http.FileServer(http.Dir(storagePath))
go func() {
http.Serve(ln, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
defer func() {
p := path.Join(storagePath, req.URL.Path)
if err := os.Remove(p); err != nil {
log.Printf("failed to remove uploaded file %s: %s", p, err)
}
}()
h.ServeHTTP(w, req)
}))
}()
return "http://" + ln.Addr().String(), nil
}