-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (57 loc) · 1.57 KB
/
main.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
package main
import (
"io"
"os"
"github.com/kataras/iris"
"github.com/iris-contrib/middleware/cors"
)
//SongInfo struct Information about converted song
type SongInfo struct{
NameOfFile string `json:"nameoffile"`
Directory string `json:"directory"`
}
const(
uploadsDir = "uploads/"
downloadsDir="downloads/"
)
//HandleSongUpload handler for recieving uploaded file
func HandleSongUpload(ctx iris.Context){
file, dataRecieved, err := ctx.FormFile("song")
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Application().Logger().Warnf("Error while uploading: %v", err.Error())
return
}
defer file.Close()
fname := dataRecieved.Filename
fileOutput, err := os.OpenFile(uploadsDir+fname,
os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Application().Logger().Warnf("Error while preparing the new file: %v", err.Error())
return
}
defer fileOutput.Close()
io.Copy(fileOutput, file)
data := SongInfo{
NameOfFile : convert(fname),
Directory : downloadsDir,
}
ctx.JSON(data)
}
//DownloadFile handler for downloading file
func DownloadFile(ctx iris.Context){
filename := ctx.Params().Get("filename")
ctx.SendFile(downloadsDir+filename,filename)
}
func main() {
Cors := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:3000","http://192.168.137.1:3000"},
AllowCredentials: true,
})
app := iris.New()
app.Use(Cors)
app.Post("/uploader", iris.LimitRequestBodySize(25<<20), HandleSongUpload)
app.Get("/downloads/:filename",DownloadFile)
app.Run(iris.Addr(":5000"))
}