-
Notifications
You must be signed in to change notification settings - Fork 0
/
vidman.go
382 lines (314 loc) · 9.27 KB
/
vidman.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"path"
"strconv"
"github.com/A29sTech/VidMan/core"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3" // This Is Ther Driver Of SQLTILE3 Only
)
// Some Importent Constents.
const (
VideoFormFeild = "video"
DocFormFeild = "doc"
)
// Globaly Acsessable Vars ;
var (
lib string
db *gorm.DB
)
///////// MOCK DATA ////////
// vid := core.ViData{
// Title: "Hello World",
// Subject: "he",
// Desc: "am",
// Tags: "niodec",
// Filename: "nonamed",
// Docname: "nodeoc",
// }
func main() {
var port string // Http server port.
var home string
var isAdmin bool
// Parse Command Line.
flag.StringVar(&lib, "lib", "", "'videopath' including Videos.db")
flag.StringVar(&port, "port", "3333", "'http-server-port' Defualt' 3333")
flag.StringVar(&home, "app", "", "-home 'index.html file require in home dir.")
flag.BoolVar(&isAdmin, "admin", false, "write, update and delete Acsess.")
flag.Parse()
// Lib Arg Check ;
if lib == "" {
fmt.Println("lib path is not provided as, -lib 'libpath'")
return
}
// Setup DataBase ;
var err error
if db, err = core.OpenViDB(path.Join(lib, "Videos.db")); err != nil {
fmt.Println(err)
return
}
// Create Mux Router ;
router := mux.NewRouter()
// Register Video Files Server ;
router.PathPrefix("/cdn/").Handler(http.StripPrefix("/cdn/",
http.FileServer(http.Dir(lib))))
// Register Api for All.
router.HandleFunc("/api/Video/{id:[0-9]+}", getVideoAPI)
router.HandleFunc("/api/SearchVideos/{colum}/{query}/{limit:[0-9]+}/{offset:[0-9]+}", searchVideoAPI)
router.
HandleFunc("/api/Videos/{limit:[0-9]+}/{offset:[0-9]+}", allVideosAPI).
Methods(http.MethodGet)
// Register if Admin Permission Given ;
if isAdmin {
router.HandleFunc("/api/AddVideo", addVideoAPI).Methods(http.MethodPost)
router.HandleFunc("/api/UpdateVideo/{id:[0-9]+}", updateVideoAPI).Methods(http.MethodPost)
router.HandleFunc("/api/DeleteVideo/{id:[0-9]+}", deleteVideoAPI)
}
// Get By Tags, Like PlayList Altranative;
router.HandleFunc("/api/PlayListTags/{tag}", getByTegs)
// Check For Home and Serve Html UI;
if home != "" {
router.HandleFunc("/", homeHandler(path.Join(home, "index.html")))
router.PathPrefix("/").
Handler(FileServerWithCustom404(http.Dir(home),
path.Join(home, "index.html")))
// router.NotFoundHandler = http.HandlerFunc(homeHandler(path.Join(home, "index.html")))
}
// Running Message
fmt.Println("Server Started : http://127.0.0.1:" + port)
// Start The Server.
http.ListenAndServe("127.0.0.1:"+port, router)
}
// FileServerWithCustom404 : For SPA ;
func FileServerWithCustom404(fs http.FileSystem, entrypoint string) http.Handler {
fsh := http.FileServer(fs)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := fs.Open(path.Clean(r.URL.Path))
if os.IsNotExist(err) {
// Call Not Found Handler ;
homeHandler(entrypoint)(w, r)
return
}
fsh.ServeHTTP(w, r)
})
}
// IndexHandler For Static Html, Javascript, Css Fire Server
func homeHandler(entrypoint string) func(w http.ResponseWriter, r *http.Request) {
fn := func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, entrypoint)
}
return http.HandlerFunc(fn)
}
// Get Video By Id ;
func getVideoAPI(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
vidata := core.ViData{}
if err := db.Table(core.TableName).First(&vidata, id).Error; err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err.Error())
}
jsonBytes, err := json.Marshal(vidata)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err.Error())
return
}
fmt.Fprintf(w, "%s", string(jsonBytes))
}
// Search Videos By Colum Name ; Default is Title;
func searchVideoAPI(w http.ResponseWriter, r *http.Request) {
// Retrive 'colum', 'query', 'limit', 'offset' from url param ;
params := mux.Vars(r)
columName, query := params["colum"], params["query"]
limit, offset := params["limit"], params["offset"]
// Craete a ViData Slice ;
visdata := []core.ViData{}
// Check geven colum name exist in database ;
if !core.ViDataHasColumName(columName) {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "ViData Table Does Not Contain Colum Name : %s", columName)
return
}
// Query Matched Data form DataBase to 'vidata' Slice ;
err := db.Table(core.TableName).
Where(columName+" LIKE ?", "%"+query+"%").
Limit(limit).
Offset(offset).
Order(columName).
Find(&visdata).Error
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Encode Result To Json ;
jsonBytes, err := json.Marshal(visdata)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Send Json Result ;
fmt.Fprintf(w, "%s", string(jsonBytes))
}
// allVideosAPI : get all videos by limit & Offset ;
func allVideosAPI(w http.ResponseWriter, r *http.Request) {
// Retrive 'limit' & 'offset' from url param ;
params := mux.Vars(r)
limit, offset := params["limit"], params["offset"]
// Create a ViData Slice ;
vidataSlice := []core.ViData{}
// Retrive All Matched Query To 'vidataSlice' ;
err := db.Table(core.TableName).Offset(offset).Limit(limit).Order("indx desc").Find(&vidataSlice).Error
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err.Error())
return
}
// Encode 'vidata' to Json ;
jsonBytes, err := json.Marshal(&vidataSlice)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err.Error())
return
}
// Finally Send The Json Results ;
fmt.Fprintf(w, string(jsonBytes))
}
// addVideoApi : Add A Video To Lib.
func addVideoAPI(w http.ResponseWriter, r *http.Request) {
// Parse Multipart Form Data.
err := r.ParseMultipartForm(10 << 20)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%s", err.Error())
return
}
// Make A VideoModel From Request Form ;
vidata, err := core.Form2ViData(r.Form)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%s", err.Error())
return
}
// Video File Handling ...
file, header, err := r.FormFile(VideoFormFeild)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%s", err.Error())
return
}
// Save File And Get FileName ;
filename := header.Filename
err = core.SaveUploadedFile(file, path.Join(lib, filename))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%s", err.Error())
return
}
// Finally Add Video To Lib ;
vidata.Filename = filename
if err = db.Table(core.TableName).Create(&vidata).Error; err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err.Error())
os.Remove(path.Join(lib, filename))
return
}
// Encode ViData to Json ;
jsonBytes, err := json.Marshal(vidata)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err.Error())
return
}
fmt.Fprintf(w, "%s", string(jsonBytes))
}
// update Databse; Require Param : id & json body ;
func updateVideoAPI(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
// Convert ID string to int ;
intID, err := strconv.Atoi(id)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%s", err.Error())
return
}
// Create A ViData Struct, to store json req data;
vidata := core.ViData{}
vidata.ID = intID
// Pasre Body To Json ;
err = json.NewDecoder(r.Body).Decode(&vidata)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%s", err.Error())
return
}
// Update To DB ;
err = db.Table(core.TableName).Model(&vidata).Omit("filename").Update(&vidata).Error
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%s", err.Error())
return
}
// Retrive Updated Data From Database ;
err = db.Table(core.TableName).First(&vidata, intID).Error
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err.Error())
return
}
// Encode ViData to Json ;
jsonBytes, err := json.Marshal(vidata)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err.Error())
return
}
// Send Encoded Json Data ;
fmt.Fprintf(w, "%s", string(jsonBytes))
}
func getByTegs(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
tag := params["tag"]
// Craete a ViData Slice ;
visdata := []core.ViData{}
// Query Matched Data form DataBase to 'vidata' Slice ;
err := db.Table(core.TableName).
Where("tags LIKE ?", "%"+tag+"%").
Order("indx").
Find(&visdata).Error
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Encode Result To Json ;
jsonBytes, err := json.Marshal(visdata)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Send Json Result ;
fmt.Fprintf(w, "%s", string(jsonBytes))
}
// deleteVideoAPI : To Delete A Video ;
func deleteVideoAPI(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
// Convert ID string to int ;
intID, err := strconv.Atoi(id)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%s", err.Error())
return
}
// Delete Op ;
err = db.Table(core.TableName).Delete(core.ViData{ID: intID}).Error
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%s", err.Error())
return
}
fmt.Fprintf(w, "%s", id)
}