aboutsummaryrefslogtreecommitdiff
path: root/fileio.go
blob: 624218542c48e0ff70712a2378a6c903766957d2 (plain)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"mime/multipart"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"

	"github.com/gabriel-vasile/mimetype"
)

func deniedPfx(pfx string) bool {
	cPfx := filepath.Clean(pfx)
	for _, p := range denyPfxs {
		if strings.HasPrefix(cPfx, p) {
			return true
		}
	}
	return false
}

func dispFile(w http.ResponseWriter, uFilePath string) {
	if deniedPfx(uFilePath) {
		htErr(w, "access", fmt.Errorf("forbidden"))
		return
	}
	fp := filepath.Clean(uFilePath)
	s := strings.Split(fp, ".")
	log.Printf("Dsiposition file=%v ext=%v", fp, s[len(s)-1])
	switch strings.ToLower(s[len(s)-1]) {
	case "url", "desktop", "webloc":
		gourl(w, fp)

	case "zip":
		listZip(w, fp)
	case "7z":
		list7z(w, fp)
	case "tar", "rar", "gz", "bz2", "xz", "tgz", "tbz2", "txz":
		listArchive(w, fp)
	case "iso":
		listIso(w, fp)

	default:
		dispInline(w, fp)
	}
}

func downFile(w http.ResponseWriter, uFilePath string) {
	if deniedPfx(uFilePath) {
		htErr(w, "access", fmt.Errorf("forbidden"))
		return
	}
	f, err := os.Stat(uFilePath)
	if err != nil {
		htErr(w, "Unable to get file attributes", err)
		return
	}
	w.Header().Set("Content-Type", "application/octet-stream")
	w.Header().Set("Content-Disposition", "attachment; filename=\""+filepath.Base(uFilePath)+"\";")
	w.Header().Set("Content-Length", fmt.Sprint(f.Size()))
	w.Header().Set("Cache-Control", *cacheCtl)
	streamFile(w, uFilePath)
}

func dispInline(w http.ResponseWriter, uFilePath string) {
	if deniedPfx(uFilePath) {
		htErr(w, "access", fmt.Errorf("forbidden"))
		return
	}
	f, err := os.Stat(uFilePath)
	if err != nil {
		htErr(w, "Unable to get file attributes", err)
		return
	}

	fi, err := os.Open(uFilePath)
	if err != nil {
		htErr(w, "Unable top open file", err)
		return
	}
	mt, err := mimetype.DetectReader(fi)
	if err != nil {
		htErr(w, "Unable to determine file type", err)
		return
	}
	fi.Close()

	w.Header().Set("Content-Type", mt.String())
	w.Header().Set("Content-Disposition", "inline")
	w.Header().Set("Content-Length", fmt.Sprint(f.Size()))
	w.Header().Set("Cache-Control", *cacheCtl)
	streamFile(w, uFilePath)
}

func streamFile(w http.ResponseWriter, uFilePath string) {
	if deniedPfx(uFilePath) {
		htErr(w, "access", fmt.Errorf("forbidden"))
		return
	}
	fi, err := os.Open(uFilePath)
	if err != nil {
		htErr(w, "Unable top open file", err)
		log.Printf("unable to read file: %v", err)
		return
	}
	defer fi.Close()

	rb := bufio.NewReader(fi)
	wb := bufio.NewWriter(w)
	bu := make([]byte, 1<<20)

	for {
		n, err := rb.Read(bu)
		if err != nil && err != io.EOF {
			htErr(w, "Unable to read file", err)
			log.Printf("unable to read file: %v", err)
			return
		}
		if n == 0 {
			break
		}
		wb.Write(bu[:n])
	}
	wb.Flush()
}

func uploadFile(w http.ResponseWriter, uDir, eSort string, h *multipart.FileHeader, f multipart.File, rw bool) {
	if !rw {
		htErr(w, "permission", fmt.Errorf("read only"))
		return
	}
	if deniedPfx(uDir) {
		htErr(w, "access", fmt.Errorf("forbidden"))
		return
	}
	defer f.Close()

	o, err := os.OpenFile(uDir+"/"+filepath.Base(h.Filename), os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		htErr(w, "unable to write file", err)
		return
	}
	defer o.Close()
	rb := bufio.NewReader(f)
	wb := bufio.NewWriter(o)
	bu := make([]byte, 1<<20)

	for {
		n, err := rb.Read(bu)
		if err != nil && err != io.EOF {
			htErr(w, "Unable to write file", err)
			return
		}
		if n == 0 {
			break
		}
		wb.Write(bu[:n])
	}
	wb.Flush()
	log.Printf("Uploaded Dir=%v File=%v Size=%v", uDir, h.Filename, h.Size)
	redirect(w, *wfmPfx+"?dir="+url.QueryEscape(uDir)+"&sort="+eSort+"&hi="+url.QueryEscape(h.Filename))
}

func saveText(w http.ResponseWriter, uDir, eSort, uFilePath, uData string, rw bool) {
	if !rw {
		htErr(w, "permission", fmt.Errorf("read only"))
		return
	}
	if deniedPfx(uDir) {
		htErr(w, "access", fmt.Errorf("forbidden"))
		return
	}
	if uData == "" {
		htErr(w, "text save", fmt.Errorf("zero lenght data"))
		return
	}
	err := ioutil.WriteFile(uFilePath+".tmp", []byte(uData), 0644)
	if err != nil {
		htErr(w, "text save", err)
		return
	}
	f, err := os.Stat(uFilePath + ".tmp")
	if err != nil {
		htErr(w, "text save", err)
		return
	}
	if f.Size() != int64(len(uData)) {
		htErr(w, "text save", fmt.Errorf("temp file size != input size"))
		return
	}
	err = os.Rename(uFilePath+".tmp", uFilePath)
	if err != nil {
		htErr(w, "text save", err)
		return
	}
	log.Printf("Saved Text Dir=%v File=%v Size=%v", uDir, uFilePath, len(uData))
	redirect(w, *wfmPfx+"?dir="+url.QueryEscape(uDir)+"&sort="+eSort+"&hi="+url.QueryEscape(filepath.Base(uFilePath)))
}

func mkdir(w http.ResponseWriter, uDir, uNewd, eSort string, rw bool) {
	if !rw {
		htErr(w, "permission", fmt.Errorf("read only"))
		return
	}
	if deniedPfx(uDir) {
		htErr(w, "access", fmt.Errorf("forbidden"))
		return
	}

	if uNewd == "" {
		htErr(w, "mkdir", fmt.Errorf("directory name is empty"))
		return
	}
	uB := filepath.Base(uNewd)
	err := os.Mkdir(uDir+"/"+uB, 0755)
	if err != nil {
		htErr(w, "mkdir", err)
		log.Printf("mkdir error: %v", err)
		return
	}
	redirect(w, *wfmPfx+"?dir="+url.QueryEscape(uDir)+"&sort="+eSort+"&hi="+url.QueryEscape(uB))
}

func mkfile(w http.ResponseWriter, uDir, uNewf, eSort string, rw bool) {
	if !rw {
		htErr(w, "permission", fmt.Errorf("read only"))
		return
	}
	if deniedPfx(uDir) {
		htErr(w, "access", fmt.Errorf("forbidden"))
		return
	}

	if uNewf == "" {
		htErr(w, "mkfile", fmt.Errorf("file name is empty"))
		return
	}
	fB := filepath.Base(uNewf)
	f, err := os.OpenFile(uDir+"/"+fB, os.O_RDWR|os.O_EXCL|os.O_CREATE, 0644)
	if err != nil {
		htErr(w, "mkfile", err)
		return
	}
	f.Close()
	redirect(w, *wfmPfx+"?dir="+url.QueryEscape(uDir)+"&sort="+eSort+"&hi="+url.QueryEscape(fB))
}

func mkurl(w http.ResponseWriter, uDir, uNewu, eUrl, eSort string, rw bool) {
	if !rw {
		htErr(w, "permission", fmt.Errorf("read only"))
		return
	}
	if deniedPfx(uDir) {
		htErr(w, "access", fmt.Errorf("forbidden"))
		return
	}
	if uNewu == "" {
		htErr(w, "mkurl", fmt.Errorf("url file name is empty"))
		return
	}
	if !strings.HasSuffix(uNewu, ".url") {
		uNewu = uNewu + ".url"
	}
	fB := filepath.Base(uNewu)
	f, err := os.OpenFile(uDir+"/"+fB, os.O_RDWR|os.O_EXCL|os.O_CREATE, 0644)
	if err != nil {
		htErr(w, "mkfile", err)
		return
	}
	// TODO(tenox): add upport for creating webloc, desktop and other formats
	fmt.Fprintf(f, "[InternetShortcut]\r\nURL=%s\r\n", eUrl)
	f.Close()
	redirect(w, *wfmPfx+"?dir="+url.QueryEscape(uDir)+"&sort="+eSort+"&hi="+url.QueryEscape(fB))
}

func renFile(w http.ResponseWriter, uDir, uBn, uNewf, eSort string, rw bool) {
	if !rw {
		htErr(w, "permission", fmt.Errorf("read only"))
		return
	}
	if deniedPfx(uDir) {
		htErr(w, "access", fmt.Errorf("forbidden"))
		return
	}

	if uBn == "" || uNewf == "" {
		htErr(w, "rename", fmt.Errorf("filename is empty"))
		return
	}
	fB := filepath.Base(uNewf)
	err := os.Rename(
		uDir+"/"+uBn,
		uDir+"/"+fB,
	)
	if err != nil {
		htErr(w, "rename", err)
		return
	}
	redirect(w, *wfmPfx+"?dir="+url.QueryEscape(uDir)+"&sort="+eSort+"&hi="+url.QueryEscape(fB))
}

func moveFiles(w http.ResponseWriter, uDir string, uFilePaths []string, uDst, eSort string, rw bool) {
	if !rw {
		htErr(w, "permission", fmt.Errorf("read only"))
		return
	}
	if deniedPfx(uDir) || deniedPfx(uDst) {
		htErr(w, "access", fmt.Errorf("forbidden"))
		return
	}

	lF := ""
	for _, f := range uFilePaths {
		fb := filepath.Base(f)
		err := os.Rename(
			uDir+"/"+fb,
			filepath.Clean(uDst+"/"+fb),
		)
		if err != nil {
			htErr(w, "move", err)
			return
		}
		lF = fb
	}
	redirect(w, *wfmPfx+"?dir="+url.QueryEscape(uDst)+"&sort="+eSort+"&hi="+url.QueryEscape(lF))
}

func deleteFiles(w http.ResponseWriter, uDir string, uFilePaths []string, eSort string, rw bool) {
	if !rw {
		htErr(w, "permission", fmt.Errorf("read only"))
		return
	}
	if deniedPfx(uDir) {
		htErr(w, "access", fmt.Errorf("forbidden"))
		return
	}

	for _, f := range uFilePaths {
		err := os.RemoveAll(uDir + "/" + filepath.Base(f))
		if err != nil {
			htErr(w, "delete", err)
			return
		}
	}
	redirect(w, *wfmPfx+"?dir="+url.QueryEscape(uDir)+"&sort="+eSort)
}