Compare commits

..

No commits in common. "main" and "v1.3.0" have entirely different histories.
main ... v1.3.0

8 changed files with 30 additions and 43 deletions

View File

@ -78,8 +78,6 @@ abyss is a basic and mostly single user http server written in go made for uploa
curl -F "file=@/path/to/file" -H "X-Auth: "$(cat /path/to/.key) http://localhost:3235/ curl -F "file=@/path/to/file" -H "X-Auth: "$(cat /path/to/.key) http://localhost:3235/
``` ```
- it is also possible to add a `-Fsecret=` to your POST to make filenames bigger and harder to guess.
- you should probably create an `alias` or a `function` to do this automatically for you. - you should probably create an `alias` or a `function` to do this automatically for you.
<details> <details>
<summary>click for an example for bash/zsh:</summary> <summary>click for an example for bash/zsh:</summary>
@ -168,7 +166,7 @@ abyss is a basic and mostly single user http server written in go made for uploa
- `UPLOAD_KEY`: this is key checked when uploading files. if the key doesn't match with server's one, then it refuses uploading. - `UPLOAD_KEY`: this is key checked when uploading files. if the key doesn't match with server's one, then it refuses uploading.
- `ABYSS_FILEDIR`: this points to the directory where abyss will save the uploads to. defaults to `./files` - `ABYSS_FILEDIR`: this points to the directory where abyss will save the uploads to. defaults to `./files`
- `ABYSS_PORT`: this is the port the server will run on. safe to leave empty. defaults to 3235 - `ABYSS_PORT`: this is the port the server will run on. safe to leave empty. defaults to 3235
- `SHOULD_AUTH`: if it is `yes`, then to upload text you will need authentication (same auth as `/tree`), any value other than that and upload is authless - `SHOULD_AUTH`: if it is `yes`, then to upload text through the browser you will need authentication (same auth as `/tree`), any value other than that and upload is auth-less
## todo: ## todo:

View File

@ -100,4 +100,12 @@ func setupHandlers(mux *http.ServeMux, app *Application) {
mux.HandleFunc("/token", BasicAuth(app.createTokenHandler, app)) mux.HandleFunc("/token", BasicAuth(app.createTokenHandler, app))
mux.HandleFunc("/files/", app.fileHandler) mux.HandleFunc("/files/", app.fileHandler)
if app.authUpload == "yes" {
mux.HandleFunc("/upload", BasicAuth(app.uploadHandler, app))
slog.Warn("text uploading through the browser will be restricted")
} else {
mux.HandleFunc("/upload", app.uploadHandler)
slog.Warn("text uploading through the browser will NOT be restricted")
}
} }

View File

@ -17,8 +17,6 @@ var extensions = map[string]string{
".png": "image", ".jpg": "image", ".jpeg": "image", ".webp": "image", ".png": "image", ".jpg": "image", ".jpeg": "image", ".webp": "image",
".mp3": "audio", ".aac": "audio", ".wav": "audio", ".flac": "audio", ".ogg": "audio",
".sh": "text", ".bash": "text", ".zsh": "text", ".sh": "text", ".bash": "text", ".zsh": "text",
".bat": "text", ".cmd": "text", ".ps1": "text", ".bat": "text", ".cmd": "text", ".ps1": "text",
".ini": "text", ".cfg": "text", ".conf": "text", ".ini": "text", ".cfg": "text", ".conf": "text",

View File

@ -43,7 +43,7 @@ AUTH_USERNAME=$AUTH_USERNAME
# This is the password of the user for accessing /tree # This is the password of the user for accessing /tree
AUTH_PASSWORD=$AUTH_PASSWORD AUTH_PASSWORD=$AUTH_PASSWORD
# This is whether you need a password to upload text (through browser or curl) # This is whether you need a password to upload text through the browser
SHOULD_AUTH=$SHOULD_AUTH SHOULD_AUTH=$SHOULD_AUTH
# This is the key needed to make uploads. Include it as X-Auth in curl. # This is the key needed to make uploads. Include it as X-Auth in curl.

View File

@ -137,11 +137,7 @@ func (app *Application) lastUploadedHandler(w http.ResponseWriter, r *http.Reque
func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
if contentType := r.Header.Get("Content-Type"); contentType == "application/x-www-form-urlencoded" { if contentType := r.Header.Get("Content-Type"); contentType == "application/x-www-form-urlencoded" {
if app.authUpload == "yes" { app.formHandler(w, r)
BasicAuth(app.formHandler, app)(w, r)
} else {
app.formHandler(w, r)
}
} else if strings.Split(contentType, ";")[0] == "multipart/form-data" { } else if strings.Split(contentType, ";")[0] == "multipart/form-data" {
app.curlHandler(w, r) app.curlHandler(w, r)
} else { } else {
@ -162,11 +158,7 @@ func (app *Application) formHandler(w http.ResponseWriter, r *http.Request) {
} }
defer file.Close() defer file.Close()
full := true filename := app.publicURL(file, ".txt")
if len(r.Form["secret"]) == 0 {
full = false
}
filename := app.publicURL(file, ".txt", full)
// reopening file because hash consumes it // reopening file because hash consumes it
file, err = os.Open("/tmp/file.txt") file, err = os.Open("/tmp/file.txt")
@ -202,11 +194,7 @@ func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) {
} }
defer file.Close() defer file.Close()
full := true filename := app.publicURL(file, filepath.Ext(handler.Filename))
if len(r.Form["secret"]) == 0 {
full = false
}
filename := app.publicURL(file, filepath.Ext(handler.Filename), full)
// reopen the file for copying, as the hash process consumed the file reader // reopen the file for copying, as the hash process consumed the file reader
file, _, err = r.FormFile("file") file, _, err = r.FormFile("file")
@ -216,15 +204,16 @@ func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) {
} }
defer file.Close() defer file.Close()
if err = SaveFile(app.lastUploadedFile, file); err != nil { err = SaveFile(app.lastUploadedFile, file)
if err != nil {
fmt.Fprintf(w, "Error parsing file: %s", err.Error()) fmt.Fprintf(w, "Error parsing file: %s", err.Error())
} }
ResponseURLHandler(w, app.url, filename) ResponseURLHandler(w, app.url, filename)
} }
func (app *Application) publicURL(file io.Reader, extension string, full bool) string { func (app *Application) publicURL(file io.Reader, extension string) string {
filename, _ := HashFile(file, extension, full) filename, _ := HashFile(file, extension)
filepath := filepath.Join(app.filesDir, filename) filepath := filepath.Join(app.filesDir, filename)

View File

@ -9,7 +9,6 @@ import (
"io" "io"
"net/http" "net/http"
"os" "os"
"strings"
"github.com/golang-jwt/jwt/v5" "github.com/golang-jwt/jwt/v5"
) )
@ -68,19 +67,17 @@ func FormatFileSize(size int64) string {
return fmt.Sprintf("%.2f GB", float64(size)/(1024*1024*1024)) return fmt.Sprintf("%.2f GB", float64(size)/(1024*1024*1024))
} }
func HashFile(file io.Reader, extension string, full bool) (string, error) { func HashFile(file io.Reader, extension string) (string, error) {
hasher := md5.New() hasher := md5.New()
if _, err := io.Copy(hasher, file); err != nil { if _, err := io.Copy(hasher, file); err != nil {
return "", err return "", err
} }
sha1Hash := strings.ToUpper(hex.EncodeToString(hasher.Sum(nil))) sha1Hash := hex.EncodeToString(hasher.Sum(nil))[:8]
filename := fmt.Sprintf("%s%s", sha1Hash, extension) filename := fmt.Sprintf("%s%s", sha1Hash, extension)
if full {
return filename, nil return filename, nil
} else {
return fmt.Sprintf("%s%s", sha1Hash[:5], extension), nil
}
} }
func SaveFile(name string, file io.Reader) error { func SaveFile(name string, file io.Reader) error {

View File

@ -20,9 +20,8 @@
</a> </a>
</div> </div>
<form action="/" method="POST"> <form action="/upload" method="POST">
<textarea name="content" placeholder="Enter your content here..."></textarea> <textarea name="content" placeholder="Enter your content here..."></textarea><br />
<br />
<button type="submit">upload</button> <button type="submit">upload</button>
</form> </form>

View File

@ -213,14 +213,12 @@
<source src="{{.Name}}" type="video/mp4" /> <source src="{{.Name}}" type="video/mp4" />
Your browser does not support the video tag. Your browser does not support the video tag.
</video> </video>
{{else if eq .Type "audio"}} {{else}}
<audio controls src="{{.Name}}"><audio /> <p>
{{else}} Couldn't detect file from extension, visit
<p> <a href="http://{{.Path}}">this link</a> to see/download your file.
Couldn't detect file from extension, visit </p>
<a href="http://{{.Path}}">this link</a> to see/download your file. {{end}}
</p>
{{end}}
</div> </div>
<footer>file uploaded in {{.TimeUploaded}}</footer> <footer>file uploaded in {{.TimeUploaded}}</footer>
</body> </body>