summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorache <ache@ache.one>2021-02-28 21:59:58 +0100
committerache <ache@ache.one>2021-02-28 21:59:58 +0100
commit94fb8f24282be3aa41f275cb21d1eca76c1899a1 (patch)
tree7e810152655d73243711d519a01cc13801cb53b7
parentMultiple upload (diff)
Config + 1.16 + MultiSelection
-rw-r--r--config/config.go17
-rw-r--r--config/config.json4
-rw-r--r--go.mod3
-rw-r--r--server.go71
-rw-r--r--src/dir.tmpl14
5 files changed, 76 insertions, 33 deletions
diff --git a/config/config.go b/config/config.go
index 48fbff4..ddf3f0a 100644
--- a/config/config.go
+++ b/config/config.go
@@ -9,15 +9,18 @@ import (
type config struct {
Hostnames []string `json:"hosts"`
Port int `json:"port"`
- AllowLocalPath bool `json:"allow-local-path"`
+ AllowHidden bool `json:"allow-hidden"`
Auth bool `json:"auth"`
RootPath string `json:"root-path"`
}
var Config config
-func ReadConfig() error {
- file, err := os.Open("config.json")
+func ReadConfig(path string) error {
+ if path == "" {
+ path = "config.json"
+ }
+ file, err := os.Open(path)
if err != nil {
return err
@@ -31,3 +34,11 @@ func ReadConfig() error {
return nil
}
+
+func SetDefaultValue() {
+ Config.Hostnames = []string{"localhost"}
+ Config.Port = 8080
+ Config.AllowHidden = false
+ Config.Auth = false
+ Config.RootPath = "."
+}
diff --git a/config/config.json b/config/config.json
index 2bd396d..74dac99 100644
--- a/config/config.json
+++ b/config/config.json
@@ -1,4 +1,6 @@
{
"host":["localhost"],
- "port": 8080
+ "port": 8080,
+ "allow-hidden": false,
+ "auth": true
}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..99339b9
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module davy
+
+go 1.16
diff --git a/server.go b/server.go
index 1bc72d2..aed70d8 100644
--- a/server.go
+++ b/server.go
@@ -9,10 +9,10 @@ import (
"html/template"
"flag"
"path"
- "syscall"
"net/url"
"errors"
- "./config"
+ "strconv"
+ "davy/config"
)
var tmpl *template.Template;
@@ -127,11 +127,16 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) {
cols = append(cols, Entry{"..", path.Dir(initPath), 0, ""})
initPath += "/"
}
+
for _, file := range fileInfo {
if file.IsDir() {
- cols = append(cols, Entry{file.Name(), initPath + file.Name(), 0, ""})
+ if config.Config.AllowHidden || file.Name()[0] != '.' {
+ cols = append(cols, Entry{file.Name(), initPath + file.Name(), 0, ""})
+ }
} else {
- files = append(files, Entry{file.Name(), initPath + file.Name(), file.Size(), normaliseSize(file.Size())})
+ if config.Config.AllowHidden || file.Name()[0] != '.' {
+ files = append(files, Entry{file.Name(), initPath + file.Name(), file.Size(), normaliseSize(file.Size())})
+ }
}
}
type Collection struct {
@@ -154,7 +159,7 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) {
func handleDelete( w http.ResponseWriter, r *http.Request) {
filename := getPath(r)
- fmt.Printf("Trying to get [%s]\n", filename)
+ fmt.Printf("Trying to delete [%s]\n", filename)
if verifyEmptyBody(w, r) != nil { return }
@@ -179,24 +184,33 @@ func handleDelete( w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent) // Default success code ¯\_(ツ)_/¯ cf : 9.6.1.8
}
func handlePut( w http.ResponseWriter, r *http.Request) {
+
+
filename := r.URL.Path[1:]
if filename == "" {
filename = "."
}
+ fmt.Printf("Trying to create [%s]\n", filename)
+
stat, err := os.Stat(path.Dir(filename))
- if err == syscall.ENOENT {
+ if os.IsNotExist(err) {
w.WriteHeader(http.StatusConflict)
fmt.Fprintf(w, "Can't find [%s]\n)", path.Dir(filename))
- return
+ fmt.Fprintf(w, "Trying to create it")
+ err := os.MkdirAll(path.Dir(filename), os.ModePerm)
+ if err != nil && os.IsNotExist(err) {
+ return
+ }
} else if err != nil{
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, err.Error())
+ fmt.Printf("Error Abording on [%s] about %s\n", filename, err)
return
- } else if ! stat.IsDir(){
+ } else if !stat.IsDir(){
w.WriteHeader(http.StatusConflict)
- fmt.Fprintln(w, "¯\\_(ツ)_/¯")
+ fmt.Fprintf(w, "¯\\_(ツ)_/¯ [Error: %s]\n", err)
return
}
@@ -380,22 +394,20 @@ func handleMethod( w http.ResponseWriter, r *http.Request) {
}
}
func main() {
- var dir string
+ var dirRoot string
var help, version bool
helpString := `This is the help`
versionString := "0.0.0"
- config.ReadConfig()
-
const (
- defaultDir = "."
+ defaultRoot = "."
usageDir = "the directory to serve"
usageHelp = "show some help"
usageVersion = "show the version"
)
- flag.StringVar(&dir, "directory", defaultDir, usageDir)
- flag.StringVar(&dir, "d", defaultDir, usageDir+" (shorthand)")
+ flag.StringVar(&dirRoot, "directory", "", usageDir)
+ flag.StringVar(&dirRoot, "d", "", usageDir + " (shorthand)")
flag.BoolVar(&help, "help", false, usageHelp)
flag.BoolVar(&help, "h", false, usageHelp+" (shorthand)")
flag.BoolVar(&version, "version", false, usageVersion)
@@ -403,12 +415,6 @@ func main() {
flag.Parse()
- fmt.Println(config.Config.Port)
-
- if dir == "" {
- dir = defaultDir
- }
-
if help {
fmt.Println(helpString)
return
@@ -418,6 +424,21 @@ func main() {
return
}
+ if config.ReadConfig("") != nil {
+ if config.ReadConfig("config/config.json") != nil {
+ fmt.Println("Config file error.")
+ fmt.Println("Charging default value")
+ config.SetDefaultValue()
+ }
+ }
+
+ if dirRoot == "" {
+ dirRoot = config.Config.RootPath
+ if dirRoot == "" {
+ dirRoot = defaultRoot
+ }
+ }
+
fmap := template.FuncMap{
"formatURL": encodeURL,
"formatSize": normaliseSize,
@@ -435,14 +456,14 @@ func main() {
http.ServeFile(w, r, baseWd + "/static/ban.svg")
})
- if err := os.Chdir(dir) ; err != nil {
- fmt.Println("Error with directory [" + dir + "]")
+ if err := os.Chdir(dirRoot) ; err != nil {
+ fmt.Println("Error with directory [" + dirRoot + "]")
return
}
- fmt.Println("Launch server")
+ fmt.Println("Launch server on port:", config.Config.Port)
http.HandleFunc("/", handleMethod)
- log.Fatal(http.ListenAndServe(":8080", nil))
+ log.Fatal(http.ListenAndServe(":" + strconv.Itoa(config.Config.Port), nil))
fmt.Println("Bye bye")
}
diff --git a/src/dir.tmpl b/src/dir.tmpl
index f88b0ed..2dceade 100644
--- a/src/dir.tmpl
+++ b/src/dir.tmpl
@@ -27,6 +27,10 @@
<label for="fileinput">Envoyer un fichier : </label><br/>
<input id="fileinput" type="file" multiple>
</div>
+ <div class="uploadDiv">
+ <label for="dirinput">Envoyer un dossier: </label><br/>
+ <input id="dirinput" type="file" directory webkitdirectory>
+ </div>
</section>
<section id="dav">
<div class="moveDiv">
@@ -52,7 +56,8 @@
<div id="footer">Create with love 💟 by <a href="https://ache.one">ache</a></div>
</div>
<script language="javascript">
-const input = document.getElementById('fileinput');
+const inputFile = document.getElementById('fileinput');
+const inputDir = document.getElementById('dirinput');
const mkcol = document.getElementById('mkcol_input');
const normSize = ( size ) => {
@@ -180,8 +185,9 @@ const upload = (file) => {
uri += '/'
}
- uri += file.name
+ uri += file.webkitRelativePath || file.name;
+ console.log(uri);
var xhr = new XMLHttpRequest();
@@ -205,7 +211,6 @@ const upload = (file) => {
let denyChil = undefined;
Array.from(rest.children).forEach( (child) => {
- console.log(child);
let tmp_Element = child;
while( tmp_Element && tmp_Element.tagName != 'A') {
tmp_Element = tmp_Element.firstElementChild;
@@ -385,7 +390,8 @@ const onPageLoad = () => {
}
mkcol.addEventListener('keydown', mkcolEvent, false);
-input.addEventListener('change', onSelectFile, false);
+inputFile.addEventListener('change', onSelectFile, false);
+inputDir.addEventListener('change', onSelectFile, false);
window.addEventListener('load', onPageLoad, false);
</script>
<style>