/
common.go
196 lines (170 loc) · 5.26 KB
/
common.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
package client
import (
"fmt"
"net/http"
"os"
"strconv"
"strings"
"syscall"
)
// ListDirectory is the Payload value from the Printer to List Directory Values to avoid filename collisions
const ListDirectory = "ListDir"
// MakeDir is the Payload value from the Printer to Make a Directory if it doesn't exit
const MakeDir = "MakeDir"
// PutFile is the Payload value from the Printer to upload a file
const PutFile = "PutFile"
// DeleteFile is the Payload value from the Printer to Delete a file normally a *LCK file
const DeleteFile = "DeleteFile"
// RemoveDir is the Payload value from the Printer to Delete a directory
const RemoveDir = "RemoveDir"
// DestDir is the Payload value from the Printer for the files dir on the filesystem
const DestDir = "destDir"
// DestName is the Payload value from the Printer for the filename on the filesystem
const DestName = "destName"
// Operation is the Payload field from the Printer for the the operation to happen, ListDir, MakeDir etc
const Operation = "theOperation"
// Sendfile is the Payload field from the Printer for the the file itself
const Sendfile = "sendfile"
// XRXNOTFOUND is the not found error message
const XRXNOTFOUND = "XRXNOTFOUND"
// XRXERROR is the default error message
const XRXERROR = "XRXERROR"
// XRXDIREXISTS is the directory exists already
const XRXDIREXISTS = "XRXDIREXISTS"
// XRXBADNAME is the filename is bad due to to FS constraints
const XRXBADNAME = "XRXBADNAME"
// XeroxApi Interface for all Printer to Server Interactions
type XeroxApi interface {
ListDirectory(directory string) (string, error)
CleanPath(directory string) string
DeleteDir(directory string) error
PutFile(r *http.Request, directory string) ([]byte, error)
MakeDirectory(directory string) error
}
// NewClient generates a new generic client for uploading
func NewClient() (XeroxApi, error) {
_, found := os.LookupEnv("google")
var x XeroxApi
if found {
gc, err := NewGoogleClient()
if err != nil {
return nil, err
}
x = gc
} else {
pgId, err := getEnvVar("PGID")
if err != nil {
return nil, err
}
userId, err := strconv.Atoi(pgId)
if err != nil {
return nil, err
}
gID, err := getEnvVar("GID")
if err != nil {
return nil, err
}
groupIp, err := strconv.Atoi(gID)
if err != nil {
return nil, err
}
x = NewFileSystemClient(userId, groupIp)
}
return x, nil
}
func getEnvVar(name string) (string, error) {
v, found := os.LookupEnv(name)
if !found {
return "", fmt.Errorf("%s must be set", name)
}
if len(v) == 0 {
return "", fmt.Errorf("%s must not be empty", name)
}
return v, nil
}
// HandleRequests takes the XeroxApi and handles all the List, Del, Remove, Put actions
func HandleRequests(x XeroxApi) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
r.ParseMultipartForm(32 << 20)
directory := x.CleanPath(strings.Join(r.PostForm[DestDir], ""))
operation := r.PostForm[Operation]
fmt.Println(fmt.Sprintf("Endpoint Hit: %s", operation))
switch strings.Join(operation, "") {
case ListDirectory:
listDirectoryAction(x, directory, w)
case MakeDir:
makeDirectoryAction(x, directory, w)
case PutFile:
message, err := x.PutFile(r, directory)
if err != nil {
w.Write([]byte(message))
}
case DeleteFile:
deleteFileAction(x, directory, w, r)
case RemoveDir:
removeDirAction(x, directory, w)
}
})
}
// listDirectoryAction handle the list directory command
func listDirectoryAction(x XeroxApi, directory string, w http.ResponseWriter) {
items, err := x.ListDirectory(directory)
if err != nil {
//log.Println(err.Error())
w.Write([]byte(XRXERROR))
} else {
w.Write([]byte(items))
}
}
// makeDirectoryAction handle the make directory command
func makeDirectoryAction(x XeroxApi, directory string, w http.ResponseWriter) {
err := x.MakeDirectory(directory)
if err != nil {
// XRXBADNAME if the name is empty.
// XRXDIREXISTS if the directory already exists.
// XRXERROR if the directory cannot be created.
switch err.(*os.PathError).Err {
case syscall.EEXIST:
w.Write([]byte(XRXDIREXISTS))
case syscall.ENOENT:
w.Write([]byte(XRXBADNAME))
default:
w.Write([]byte(XRXERROR))
}
}
}
// deleteFileAction handle the delete file from FS
func deleteFileAction(x XeroxApi, directory string, w http.ResponseWriter, r *http.Request) {
// XRXNOTFOUND if the requested file isn't found.
// XRXERROR the file cannot be deleted.
destinationName := r.PostForm[DestName]
filename := strings.Join(destinationName, "")
if strings.Join(destinationName, "") != "" {
directory = fmt.Sprintf("%s%s", directory, filename)
}
err := x.DeleteDir(directory)
if err != nil {
switch err.(*os.PathError).Err {
case syscall.ENOENT:
w.Write([]byte(XRXNOTFOUND))
default:
w.Write([]byte(XRXERROR))
}
}
}
// removeDirAction handle the delete folder from FS
func removeDirAction(x XeroxApi, directory string, w http.ResponseWriter) {
// XRXBADNAME if the requested file isn't of the correct type or the name is empty.
// XRXNOTFOUND if the requested file isn't found.
// XRXERROR the file cannot be deleted.
err := x.DeleteDir(directory)
if err != nil {
switch err.(*os.PathError).Err {
case syscall.ENOENT:
w.Write([]byte(XRXNOTFOUND))
default:
w.Write([]byte(XRXERROR))
}
}
}