This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
210 lines (180 loc) · 6.42 KB
/
handlers.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
//Written by Lukas Marckmiller
//This file contains the handler funcs for defined rest routes.
package main
import (
"github.com/gin-gonic/gin"
"github.com/jaypipes/ghw"
"github.com/semihalev/gin-stats"
"net/http"
"net/url"
"strconv"
"time"
)
//Cache for all active jobs
var jobs = map[string]*ImageJob{}
var imageJobError error
func showIndexPage(context *gin.Context) {
context.HTML(
http.StatusOK,
"index.html",
gin.H{
"title": "Welcome",
})
}
//Handler for network bandwidth check, decides which imager is used for transmission and which output location
func getIsRemoteTransferPossible(context *gin.Context) {
var device DevicePresentation
var cachedOptions ImageOption
if err := context.BindJSON(&device); err != nil {
context.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusBadRequest, "message": "Bad request format."})
return
}
cachedOptions = device.ImageOptionsPresentation.ImageOption
estimatedTime, err := netcheck(device.Size, device.Name)
if err != nil {
cachedOptions.Target = Local
} else {
cachedOptions.Target = Remote
fullImageTransfer := validate(estimatedTime)
/*
time := estimatedTime
h := time / 60 / 60
time -= h * 60 * 60
m := time / 60
fmt.Printf("Estimated time %02d:%02d\n", h, m)
*/
if fullImageTransfer {
cachedOptions.Type = Full
} else {
cachedOptions.Type = Part
}
}
context.JSON(http.StatusOK, &ImageOptionsPresentation{cachedOptions, estimatedTime})
}
//Handler returns all plugged in block devices
func getMedia(context *gin.Context) {
err, disks := getDisksWithoutBootPart()
if err != nil {
context.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError, "message": "Error retrieving device information."})
return
}
context.JSON(http.StatusOK, disks)
}
//Handler returns free,used bytes for mountpoint
func getDiskSpaceStatus(context *gin.Context) {
path := context.Params.ByName("path")
path, _ = url.QueryUnescape(path)
path, _ = strconv.Unquote(path)
context.JSON(http.StatusOK, getAvailableDiskSpace(path))
}
//Handler returns a list of mounted gwh.Partitions
func getMountedMedia(context *gin.Context) {
err, parts := getMountPointsWithoutBoot()
if err != nil {
context.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError, "message": "Error retrieving device information."})
return
}
context.JSON(http.StatusOK, parts)
}
//Handler returns mounted ghw.Partition with id
func getMountedMediaById(context *gin.Context) {
err, disks := getDisksWithoutBootPart()
if err != nil {
context.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError, "message": "Error retrieving device information."})
return
}
paramId := context.Params.ByName("id")
id, err := strconv.Atoi(paramId)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "Bad input value for parameter id, not an integer."})
return
}
if id >= len(disks) {
context.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "Bad input value for parameter id, index out of bounds."})
return
}
context.JSON(http.StatusOK, disks[id])
}
//Handler starts imaging process and verify hashes
func createAndStartImageJob(context *gin.Context) {
//Check disk write estimated time and set to ImageJobOptions -> part if low writetime and full if good write time
var imageJobRequestPresentation ImageJobRequestPresentation
if err := context.BindJSON(&imageJobRequestPresentation); err != nil {
context.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "Bad input value for imageJob."})
return
}
devPath := imageJobRequestPresentation.Path
cachedOptions := imageJobRequestPresentation.ImageOption
mountTarget := imageJobRequestPresentation.Mount
imgName := app.DeviceName + time.Now().Format("20060102MST030405PM")
job := ImageJob{Id: imgName, Option: cachedOptions}
go func() {
imageJobError = job.run(devPath, mountTarget, imgName)
}()
jobs[job.Id] = &job
context.JSON(http.StatusOK, job.Id)
}
//Handler aborts image process with id
func cancelImageJob(context *gin.Context) {
elem, ok := jobs[context.Param("id")]
if !ok {
context.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "Bad input value for parameter id, no image job for id."})
return
}
if err := elem.cancel(); err != nil {
context.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError, "message": "Error while canceling image job."})
return
}
delete(jobs, context.Param("id"))
context.Status(http.StatusOK)
return
}
//Handler returns image job which contains progress information and stats.
func getImageJobById(context *gin.Context) {
elem, ok := jobs[context.Param("id")]
if !ok {
context.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "Bad input value for parameter id, no image job for id."})
return
}
var imageJobErrorText string
if imageJobError != nil {
imageJobErrorText = imageJobError.Error()
}
inputFileOut, outputFileOut := elem.getCachedOutput()
context.JSON(http.StatusOK, ImageJobPresentation{
CommandOfOutput: outputFileOut,
CommandIfOutput: inputFileOut,
Running: elem.Running,
Id: elem.Id,
Error: imageJobErrorText,
Hashes: elem.Hashes,
HashResult: elem.HashResult})
}
//Handler for stats middleware.
func getStatInfo(context *gin.Context) {
context.JSON(http.StatusOK, stats.Report())
}
//TODO Implement cache cleaning for ImageJobs
type ImageJobPresentation struct {
CommandOfOutput string `json:"commandOfOutput"`
CommandIfOutput string `json:"commandIfOutput"`
Running bool `json:"running"`
Id string `json:"id"`
Error string `json:"error"`
Hashes Hashes `json:"hashes"`
HashResult HashResult `json:"hash_result"`
}
type ImageJobRequestPresentation struct {
Path string `json:"path"`
ImageOption ImageOption `json:"image_option"`
Mount ghw.Partition `json:"mount"`
}
type DevicePresentation struct {
Name string `json:"name"`
Size int64 `json:"size"`
ImageOptionsPresentation ImageOptionsPresentation `json:"image_options_presentation"`
}
type ImageOptionsPresentation struct {
ImageOption ImageOption `json:"image_option"`
EstimatedSecs int32 `json:"estimated_secs"`
}