feat: add download page
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
"github.com/valkey-io/valkey-go"
|
||||
)
|
||||
|
||||
func DownloadFile(client valkey.Client) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
upload_id := r.PathValue("upload_id")
|
||||
if upload_id == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
file_id := r.PathValue("file_id")
|
||||
if file_id == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
files_json, err := client.Do(r.Context(),
|
||||
client.B().Get().Key(upload_id).Build()).ToString()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
var files []string
|
||||
err = json.Unmarshal([]byte(files_json), &files)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
found := false
|
||||
found_id := 0
|
||||
for i, id := range files {
|
||||
if id == file_id {
|
||||
found = true
|
||||
found_id = i
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
file := files[found_id]
|
||||
|
||||
filename, err := client.Do(r.Context(), client.B().Get().Key(file+":filename").Build()).ToString()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fileContents, err := client.Do(r.Context(), client.B().Get().Key(file+":contents").Build()).AsBytes()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
||||
mime := mimetype.Detect(fileContents)
|
||||
w.Header().Add("mimetype", mime.String())
|
||||
w.Write(fileContents)
|
||||
}
|
||||
}
|
||||
func DownloadFilesZipped(client valkey.Client) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
upload_id := r.PathValue("upload_id")
|
||||
if upload_id == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
files_json, err := client.Do(r.Context(),
|
||||
client.B().Get().Key(upload_id).Build()).ToString()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
var files []string
|
||||
|
||||
err = json.Unmarshal([]byte(files_json), &files)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if len(files) == 1 {
|
||||
file := files[0]
|
||||
|
||||
filename, err := client.Do(r.Context(), client.B().Get().Key(file+":filename").Build()).ToString()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fileContents, err := client.Do(r.Context(), client.B().Get().Key(file+":contents").Build()).AsBytes()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
||||
mime := mimetype.Detect(fileContents)
|
||||
w.Header().Add("mimetype", mime.String())
|
||||
w.Write(fileContents)
|
||||
return
|
||||
}
|
||||
|
||||
zipBuf := new(bytes.Buffer)
|
||||
zipWriter := zip.NewWriter(zipBuf)
|
||||
|
||||
for _, file := range files {
|
||||
filename, err := client.Do(r.Context(), client.B().Get().Key(file+":filename").Build()).ToString()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
fileContents, err := client.Do(r.Context(), client.B().Get().Key(file+":contents").Build()).AsBytes()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
w, err := zipWriter.Create(filename)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
w.Write(fileContents)
|
||||
|
||||
}
|
||||
|
||||
zipWriter.Close()
|
||||
w.Header().Add("mimetype", "application/zip")
|
||||
w.Header().Add("Content-Disposition", `attachment; filename="timeshare-download.zip"`)
|
||||
io.Copy(w, zipBuf)
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.hafen.run/lukas/timeshare/pages"
|
||||
"github.com/a-h/templ"
|
||||
"github.com/valkey-io/valkey-go"
|
||||
)
|
||||
|
||||
func ListFiles(client valkey.Client) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
upload_id := r.PathValue("upload_id")
|
||||
if upload_id == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
expiresInt, err := client.Do(r.Context(), client.B().Ttl().Key(upload_id).Build()).AsInt64()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
expiresIn := time.Second * time.Duration(expiresInt)
|
||||
expires := time.Now().Add(expiresIn)
|
||||
|
||||
files_json, err := client.Do(r.Context(),
|
||||
client.B().Get().Key(upload_id).Build()).ToString()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
var fileIds []string
|
||||
|
||||
err = json.Unmarshal([]byte(files_json), &fileIds)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
files := make([]pages.File, len(fileIds))
|
||||
for i, fileid := range fileIds {
|
||||
filename, err := client.Do(r.Context(), client.B().Get().Key(fileid+":filename").Build()).ToString()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
files[i] = pages.File{
|
||||
Filename: filename,
|
||||
Key: fileid,
|
||||
}
|
||||
}
|
||||
|
||||
templ.Handler(pages.ListShareContents(expires, upload_id, files)).ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.hafen.run/lukas/timeshare/pages"
|
||||
"github.com/a-h/templ"
|
||||
"github.com/google/uuid"
|
||||
"github.com/valkey-io/valkey-go"
|
||||
)
|
||||
|
||||
func UploadFiles(client valkey.Client) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseMultipartForm(125_000_000) // 1G max memory usage
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
form := r.MultipartForm
|
||||
expiry_values := form.Value["expiry"]
|
||||
expiry, err := time.ParseDuration(expiry_values[0])
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
form_files := form.File["files"]
|
||||
files := make([]string, len(form_files))
|
||||
for i := range form_files {
|
||||
fid := uuid.New().String()
|
||||
files[i] = fid
|
||||
f, err := form_files[i].Open()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
_, err = io.Copy(buf, f)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
err = client.Do(r.Context(), client.B().Set().Key(fid+":filename").Value(form_files[i].Filename).Ex(expiry).Build()).Error()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
err = client.Do(r.Context(), client.B().Set().Key(fid+":contents").Value(valkey.BinaryString(buf.Bytes())).Ex(expiry).Build()).Error()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
uid := uuid.New()
|
||||
files_b, _ := json.Marshal(files)
|
||||
err = client.Do(r.Context(), client.B().Set().Key(uid.String()).Value(string(files_b)).Ex(expiry).Build()).Error()
|
||||
|
||||
templ.Handler(pages.Upload([]pages.Expiry{
|
||||
{DurationCode: "24h", DurationName: "24 Hours"},
|
||||
{DurationCode: "36h", DurationName: "36 Hours"},
|
||||
{DurationCode: "48h", DurationName: "48 Hours"},
|
||||
{DurationCode: "168h", DurationName: "1 Week"},
|
||||
}, fmt.Sprintf("https://share.lukaswerner.com/%s", uid.String()))).ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user