Skip to content

Commit 73f03b7

Browse files
committed
Allow uploading multiple files at once
from sgreben#11
1 parent 44ad9e5 commit 73f03b7

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ You can set Basic authorization for a single route using the format `user:passwd
131131
$ ./http-file-server admin:1234@/home=/tmp/home
132132
```
133133

134-
Nore: The `--user` and `--passwd` arguments will be ignored.
134+
Note: The `--user` and `--passwd` arguments will be ignored.
135135

136136
For example:
137137
```sh

main.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,16 @@ func main() {
101101

102102
func server(addr string, routes routes) error {
103103
// check exist folder templates
104-
if stat, err := os.Stat(customTemplateFlag); os.IsNotExist(err) || stat.IsDir() == false {
105-
log.Printf("Wrong path to folder with custom templates: %s\n", customTemplateFlag)
106-
customTemplateFlag = ""
107-
} else {
108-
if string(customTemplateFlag[len(customTemplateFlag)-1]) == osPathSeparator {
109-
customTemplateFlag = strings.TrimSuffix(customTemplateFlag, osPathSeparator)
104+
if customTemplateFlag != "" {
105+
if stat, err := os.Stat(customTemplateFlag); os.IsNotExist(err) || stat.IsDir() == false {
106+
log.Printf("Wrong path to folder with custom templates: %s\n", customTemplateFlag)
107+
customTemplateFlag = ""
108+
} else {
109+
if string(customTemplateFlag[len(customTemplateFlag)-1]) == osPathSeparator {
110+
customTemplateFlag = strings.TrimSuffix(customTemplateFlag, osPathSeparator)
111+
}
112+
log.Printf("Added custom templates: %s", customTemplateFlag)
110113
}
111-
log.Printf("Added custom templates: %s", customTemplateFlag)
112114
}
113115

114116
mux := http.DefaultServeMux

server.go

+20-19
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package main
22

33
import (
44
"github.com/dastoori/higgs"
5+
"io"
56
"path/filepath"
67
"strconv"
78
)
89

910
import (
1011
"fmt"
1112
"html/template"
12-
"io"
1313
"log"
1414
"math"
1515
"net/http"
@@ -75,7 +75,7 @@ const directoryListingTemplateText = `
7575
</tr>
7676
{{- end }}
7777
{{- if .AllowUpload }}
78-
<tr><td colspan=4><form method="post" enctype="multipart/form-data"><input required name="file" type="file"/><input value="Upload" type="submit"/></form></td></tr>
78+
<tr><td colspan=4><form method="post" enctype="multipart/form-data"><input required name="file" type="file multiple"/><input value="Upload" type="submit"/></form></td></tr>
7979
{{- end }}
8080
</tbody>
8181
</table>
@@ -311,26 +311,27 @@ func (f *fileHandler) serveDir(w http.ResponseWriter, r *http.Request, osPath st
311311
}
312312

313313
func (f *fileHandler) serveUploadTo(w http.ResponseWriter, r *http.Request, osPath string) error {
314-
if err := r.ParseForm(); err != nil {
315-
return err
316-
}
317-
in, h, err := r.FormFile("file")
318-
if err == http.ErrMissingFile {
319-
w.Header().Set("Location", r.URL.String())
320-
w.WriteHeader(303)
321-
}
322-
if err != nil {
323-
return err
324-
}
325-
outPath := filepath.Join(osPath, filepath.Base(h.Filename))
326-
out, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY, 0600)
314+
mr, err := r.MultipartReader()
327315
if err != nil {
328316
return err
329317
}
330-
defer out.Close()
331-
332-
if _, err := io.Copy(out, in); err != nil {
333-
return err
318+
for {
319+
part, err := mr.NextPart()
320+
if err == io.EOF {
321+
break
322+
} else if err != nil {
323+
return err
324+
} else if part.FormName() == "file" {
325+
outPath := filepath.Join(osPath, filepath.Base(part.FileName()))
326+
out, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY, 0600)
327+
if err != nil {
328+
return err
329+
}
330+
defer out.Close()
331+
if _, err := io.Copy(out, part); err != nil {
332+
return err
333+
}
334+
}
334335
}
335336
w.Header().Set("Location", r.URL.String())
336337
w.WriteHeader(303)

templates/base.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
<form class="row m-0" method="post" enctype="multipart/form-data">
107107
<div class="input-group mb-3">
108108
<input class="form-control form-control" id="formFileSm" required name="file" type="file"
109-
aria-describedby="button-upload">
109+
multiple aria-describedby="button-upload">
110110
<button type="submit" class="btn btn-outline-success" value="Upload" id="button-upload">
111111
<i class="bi bi-cloud-plus" data-toggle="tooltip" title="Upload"></i>
112112
</button>

0 commit comments

Comments
 (0)