-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleaner.go
107 lines (89 loc) · 2.55 KB
/
cleaner.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
package main
//run command via go run cleaner.go --filepath=/Users/caveman/Desktop
import (
"fmt"
"github.com/julienschmidt/httprouter"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"syscall"
"time"
)
type MetaData struct {
LastAccessed, LastModiefied, FileName, FullFilePath string
}
var fileInfoPageTmpl, err = template.ParseFiles("templates/index.html")
func unixTimeToDate(lastAccess string) time.Time {
i, err := strconv.ParseInt(lastAccess, 10, 64)
if err != nil {
panic(err)
}
tm := time.Unix(i, 0)
return tm
}
func fileInfo(fileName string) MetaData {
// for time formatting in golang
// details: http://golang.org/pkg/time/#Time.Format and
// https://gobyexample.com/time-formatting-parsing
const layout = "2006-01-02"
var fileMeta MetaData
file, error := os.Stat(fileName)
if error != nil {
fmt.Println(error)
}
lastAccess := (file.Sys().(*syscall.Stat_t).Atimespec.Sec)
// dirty hack fix it via actively coverting or using sth else than ParseInt!!
dt := unixTimeToDate(fmt.Sprint(lastAccess))
fileMeta.LastAccessed = dt.Format(layout)
fileMeta.LastModiefied = file.ModTime().Format(layout)
fileMeta.FileName = file.Name()
fileMeta.FullFilePath = fileName
return fileMeta
}
func listFiles(w http.ResponseWriter, filePath string, r *http.Request) {
files, _ := ioutil.ReadDir(filePath)
allFiles := []MetaData{}
for _, f := range files {
fileName := f.Name()
FullFilePath := filePath + fileName
fileMeta := fileInfo(FullFilePath)
singleFileInfo := MetaData{fileMeta.LastAccessed, fileMeta.LastModiefied, fileMeta.FileName, fileMeta.FullFilePath}
allFiles = append(allFiles, singleFileInfo)
}
// dirty?
if r.FormValue("filename") != "" {
for _, value := range r.Form {
for i := 0; i < len(value); i++ {
if value[i] != "" {
fmt.Println("Delteing", value[i])
err := os.Remove(value[i])
if err != nil {
fmt.Println(err)
return
}
}
}
}
}
if err := fileInfoPageTmpl.Execute(w, allFiles); err != nil {
fmt.Println("Failed to build page", err)
}
}
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome to MAC Cleaner!\n")
}
func prepareCleaning(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
filePath := ps.ByName("path")
listFiles(w, filePath, r)
}
func main() {
fmt.Println("Mac cleaner running on port 8080")
router := httprouter.New()
router.GET("/", Index)
router.GET("/cleaner/*path", prepareCleaning)
router.POST("/cleaner/*path", prepareCleaning)
log.Fatal(http.ListenAndServe(":8080", router))
}