forked from drmly/drmly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
215 lines (201 loc) · 5.88 KB
/
util.go
File metadata and controls
215 lines (201 loc) · 5.88 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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/skratchdot/open-golang/open"
haikunator "github.com/yelinaung/go-haikunator"
filetype "gopkg.in/h2non/filetype.v1"
)
// Log is exported to not conflict w/ log(which gofmt was giving me troubles with when using with VSCode )
var Log = logrus.New()
var currentUser string
func init() {
// let's log output for later grepping
// You could set this to any `io.Writer` such as a file
// file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666)
// if err == nil {
// Log.Out = file
// } else {
// Log.Info("Failed to log to file, using default stderr")
// }
open.Run("http://localhost:8080")
// set some common variables needed by dream()
cmd, err := exec.Command("who").CombinedOutput()
if err != nil {
Log.Error("failed to know who is running the app, err: ", err)
}
currentUser = strings.Split(string(cmd), " ")[0]
basePath = fmt.Sprintf("/Users/%s/Desktop/bind", currentUser)
}
// makes sure we have our working dir to place all our files
func ensureBindDirs() error {
user, err := user.Current()
if err != nil {
Log.Fatal(err)
}
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
Log.Fatal(err)
}
Log.Info("dir of bind is ", dir)
Log.Info("Hello " + user.Name + " <3100LTC")
Log.Info("====")
Log.Info("Id: " + user.Uid)
Log.Info("Username: " + user.Username)
Log.Info("Home Dir: " + user.HomeDir)
Log.Info("this is the normal user: ", currentUser)
basePath := fmt.Sprintf("/Users/%s/Desktop/bind", currentUser)
// Log.Info("dir: ", usr, " and expanded dir: ", exp, " and basePath to be working from is ", basePath)
Log.Info("bind folder basePath: ", basePath)
if _, err := os.Stat(basePath); os.IsNotExist(err) {
err := os.Mkdir(basePath, 0777)
if err != nil {
Log.Error("failed os.Mkdir", err)
}
Log.Info("bind FOLDER was created")
}
// also make dirs that live inside it
frames := fmt.Sprintf("%s/frames", basePath)
if _, err := os.Stat(frames); os.IsNotExist(err) {
err := os.Mkdir(frames, 0777)
if err != nil {
Log.Error("failed os.Mkdir", err)
}
Log.Info("bind frames was created")
}
audio := fmt.Sprintf("%s/audio", basePath)
if _, err := os.Stat(audio); os.IsNotExist(err) {
err := os.Mkdir(audio, 0777)
if err != nil {
Log.Error("failed os.Mkdir", err)
}
Log.Info("bind audio was created")
}
videos := fmt.Sprintf("%s/videos", basePath)
if _, err := os.Stat(videos); os.IsNotExist(err) {
err := os.Mkdir(videos, 0777)
if err != nil {
Log.Error("failed os.Mkdir", err)
}
Log.Info("bind video was created")
}
logs := fmt.Sprintf("%s/logs", basePath)
if _, err := os.Stat(logs); os.IsNotExist(err) {
err := os.Mkdir(logs, 0777)
if err != nil {
Log.Error("failed os.Mkdir", err)
}
Log.Info("bind logs was created")
}
return nil
}
// saveFile will save whatever file it can
func saveFile(c *gin.Context) (string, string, error) {
file, err := c.FormFile("file")
if err != nil {
Log.Info("failed to get file", err)
}
name := strings.Split(file.Filename, ".")[0]
path := fmt.Sprintf("%s/frames/%s", basePath, name)
if alreadyHave(path) {
name = renamer(name)
path = fmt.Sprintf("$HOME/Desktop/%s", name)
Log.Info("\nwe renamed as: ", name)
}
// updateUser(name, c)
// save the file
savedFile := fmt.Sprintf("frames/%s/%s", name, file.Filename)
if err := c.SaveUploadedFile(file, savedFile); err != nil {
// c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
Log.Fatal("failed to save", err)
}
return "", "", errors.New("failed to save file")
}
// checkFile sort out what kind of file it is and if we support it, else error
func checkFile(c *gin.Context) (string, error) {
reader, _, err := c.Request.FormFile("file")
if err != nil {
Log.Info("can't get file from form", err)
c.String(200, "file missing from upload, please try again with a file ")
return "", errors.New("file missing from upload")
}
// check if it's really a gif
fmt.Print("checking what kind of file")
// Log.Println(uploadFile.Filename)
buf, err := ioutil.ReadAll(reader)
if err != nil {
Log.Print("not buffered")
}
kind, unknown := filetype.Match(buf)
if unknown != nil {
Log.Info("Unknown file type: %s", unknown)
return "", errors.New("bad file type, I can't work with it: ,")
} else if kind.Extension == "video/mp4" {
Log.Info("it's a video!, todo: implement")
return "video/mp4", nil
} else if kind.Extension == "image/gif" {
return "image/gif", nil
} else if kind.Extension == "image/jpg" {
return "image/jpg", nil
} else {
return "", errors.New("don't knwo what file this is ")
}
}
// howManyOf returns list of .ext at a path
func howManyOf(ext string, pathS string) int {
list := make([]string, 0, 100)
// get all gifs in deepgif dir
err := filepath.Walk(pathS, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if filepath.Ext(path) == ext { //like .mp4 or .mov or .png
Log.Info(path)
list = append(list, path)
}
return nil
})
if err != nil {
Log.Infof("walk error [%v]\n", err)
}
return len(list)
}
// deepGIFFIles returns list of gifs in dir deepgif
func deepGIFFiles() []string {
list := make([]string, 0, 100)
// get all gifs in deepgif dir
err := filepath.Walk("deepgif", func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if filepath.Ext(path) == ".mp4" {
Log.Info(path)
list = append(list, path)
}
return nil
})
if err != nil {
fmt.Printf("walk error [%v]\n", err)
}
return list
}
func alreadyHave(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
func renamer(n string) string {
fmt.Print("\n We had to rename the file")
h := haikunator.New(time.Now().UTC().UnixNano())
return fmt.Sprintf("%s%s", n, h.Haikunate())
}