2023-02-20 20:25:38 +01:00
|
|
|
/*
|
|
|
|
|
Copyright © 2023 Thomas von Dein
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-24 12:16:03 +01:00
|
|
|
package api
|
2023-02-20 20:25:38 +01:00
|
|
|
|
|
|
|
|
import (
|
2023-02-24 12:16:03 +01:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2023-02-20 20:25:38 +01:00
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/tlinden/up/upd/cfg"
|
2023-02-28 19:05:09 +01:00
|
|
|
|
2023-02-20 20:25:38 +01:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2023-03-01 13:31:44 +01:00
|
|
|
func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *Db) (string, error) {
|
2023-02-20 20:25:38 +01:00
|
|
|
// supports upload of multiple files with:
|
|
|
|
|
//
|
|
|
|
|
// curl -X POST localhost:8080/putfile \
|
|
|
|
|
// -F "upload[]=@/home/scip/2023-02-06_10-51.png" \
|
|
|
|
|
// -F "upload[]=@/home/scip/pgstat.png" \
|
|
|
|
|
// -H "Content-Type: multipart/form-data"
|
|
|
|
|
//
|
|
|
|
|
// If multiple files are uploaded, they are zipped, otherwise
|
|
|
|
|
// the file is being stored as is.
|
|
|
|
|
//
|
|
|
|
|
// Returns the name of the uploaded file.
|
2023-03-06 19:46:06 +01:00
|
|
|
|
2023-02-20 20:25:38 +01:00
|
|
|
id := uuid.NewString()
|
|
|
|
|
|
|
|
|
|
var returnUrl string
|
|
|
|
|
var formdata Meta
|
|
|
|
|
|
|
|
|
|
os.MkdirAll(filepath.Join(cfg.StorageDir, id), os.ModePerm)
|
|
|
|
|
|
|
|
|
|
// fetch auxiliary form data
|
2023-02-24 12:16:03 +01:00
|
|
|
form, err := c.MultipartForm()
|
|
|
|
|
if err != nil {
|
|
|
|
|
Log("multipart error %s", err.Error())
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2023-02-21 12:43:10 +01:00
|
|
|
|
2023-02-20 20:25:38 +01:00
|
|
|
// init upload obj
|
2023-03-01 19:48:49 +01:00
|
|
|
entry := &Upload{Id: id, Uploaded: Timestamp{Time: time.Now()}}
|
2023-02-20 20:25:38 +01:00
|
|
|
|
|
|
|
|
// retrieve files, if any
|
|
|
|
|
files := form.File["upload[]"]
|
2023-03-02 18:31:07 +01:00
|
|
|
members, err := SaveFormFiles(c, cfg, files, id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fiber.NewError(500, "Could not store uploaded file[s]!")
|
2023-02-20 20:25:38 +01:00
|
|
|
}
|
2023-03-02 18:31:07 +01:00
|
|
|
entry.Members = members
|
2023-02-20 20:25:38 +01:00
|
|
|
|
2023-03-02 18:31:07 +01:00
|
|
|
// extract auxilliary form data (expire field et al)
|
2023-02-24 12:16:03 +01:00
|
|
|
if err := c.BodyParser(&formdata); err != nil {
|
|
|
|
|
Log("bodyparser error %s", err.Error())
|
2023-02-21 12:43:10 +01:00
|
|
|
return "", err
|
|
|
|
|
}
|
2023-02-24 12:16:03 +01:00
|
|
|
|
2023-03-02 18:31:07 +01:00
|
|
|
// post process expire
|
2023-02-21 12:43:10 +01:00
|
|
|
if len(formdata.Expire) == 0 {
|
|
|
|
|
entry.Expire = "asap"
|
|
|
|
|
} else {
|
2023-03-02 18:31:07 +01:00
|
|
|
ex, err := Untaint(formdata.Expire, `[^dhms0-9]`) // duration or asap allowed
|
2023-03-01 19:48:49 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
entry.Expire = ex
|
2023-02-21 12:43:10 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-02 18:31:07 +01:00
|
|
|
// get url [and zip if there are multiple files]
|
|
|
|
|
returnUrl, Newfilename, err := ProcessFormFiles(cfg, entry.Members, id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fiber.NewError(500, "Could not process uploaded file[s]!")
|
2023-02-20 20:25:38 +01:00
|
|
|
}
|
2023-03-02 18:31:07 +01:00
|
|
|
entry.File = Newfilename
|
2023-02-20 20:25:38 +01:00
|
|
|
|
|
|
|
|
Log("Now serving %s from %s/%s", returnUrl, cfg.StorageDir, id)
|
|
|
|
|
Log("Expire set to: %s", entry.Expire)
|
|
|
|
|
|
2023-02-28 19:05:09 +01:00
|
|
|
// we do this in the background to not thwart the server
|
2023-03-01 13:31:44 +01:00
|
|
|
go db.Insert(id, entry)
|
2023-02-20 20:25:38 +01:00
|
|
|
|
2023-02-28 19:05:09 +01:00
|
|
|
return returnUrl, nil
|
|
|
|
|
}
|
2023-02-20 20:25:38 +01:00
|
|
|
|
2023-03-06 19:46:06 +01:00
|
|
|
func FileGet(c *fiber.Ctx, cfg *cfg.Config, db *Db, shallExpire ...bool) error {
|
2023-03-01 19:48:49 +01:00
|
|
|
// deliver a file and delete it if expire is set to asap
|
2023-02-28 19:05:09 +01:00
|
|
|
|
2023-03-01 19:48:49 +01:00
|
|
|
// we ignore c.Params("file"), cause it may be malign. Also we've
|
|
|
|
|
// got it in the db anyway
|
|
|
|
|
id, err := Untaint(c.Params("id"), `[^a-zA-Z0-9\-]`)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fiber.NewError(403, "Invalid id provided!")
|
|
|
|
|
}
|
2023-02-28 19:05:09 +01:00
|
|
|
|
2023-03-01 13:31:44 +01:00
|
|
|
upload, err := db.Lookup(id)
|
2023-02-28 19:05:09 +01:00
|
|
|
if err != nil {
|
|
|
|
|
// non existent db entry with that id, or other db error, see logs
|
|
|
|
|
return fiber.NewError(404, "No download with that id could be found!")
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 19:48:49 +01:00
|
|
|
file := upload.File
|
2023-02-28 19:05:09 +01:00
|
|
|
filename := filepath.Join(cfg.StorageDir, id, file)
|
2023-02-20 20:25:38 +01:00
|
|
|
|
2023-02-28 19:05:09 +01:00
|
|
|
if _, err := os.Stat(filename); err != nil {
|
|
|
|
|
// db entry is there, but file isn't (anymore?)
|
2023-03-01 13:31:44 +01:00
|
|
|
go db.Delete(id)
|
2023-03-02 18:31:07 +01:00
|
|
|
return fiber.NewError(404, "No download with that id could be found!")
|
2023-02-28 19:05:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// finally put the file to the client
|
|
|
|
|
err = c.Download(filename, file)
|
|
|
|
|
|
2023-03-06 19:46:06 +01:00
|
|
|
if len(shallExpire) > 0 {
|
|
|
|
|
if shallExpire[0] == true {
|
|
|
|
|
go func() {
|
|
|
|
|
// check if we need to delete the file now and do it in the background
|
|
|
|
|
if upload.Expire == "asap" {
|
|
|
|
|
cleanup(filepath.Join(cfg.StorageDir, id))
|
|
|
|
|
db.Delete(id)
|
|
|
|
|
}
|
|
|
|
|
}()
|
2023-02-20 20:25:38 +01:00
|
|
|
}
|
2023-03-06 19:46:06 +01:00
|
|
|
}
|
2023-02-20 20:25:38 +01:00
|
|
|
|
2023-02-28 19:05:09 +01:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Id struct {
|
|
|
|
|
Id string `json:"name" xml:"name" form:"name"`
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 13:31:44 +01:00
|
|
|
func FileDelete(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
|
2023-02-28 19:05:09 +01:00
|
|
|
// delete file, id dir and db entry
|
|
|
|
|
|
2023-03-01 19:48:49 +01:00
|
|
|
id, err := Untaint(c.Params("id"), `[^a-zA-Z0-9\-]`)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fiber.NewError(403, "Invalid id provided!")
|
|
|
|
|
}
|
2023-02-28 19:05:09 +01:00
|
|
|
|
|
|
|
|
if len(id) == 0 {
|
2023-03-02 18:31:07 +01:00
|
|
|
return fiber.NewError(403, "No id given!")
|
2023-02-28 19:05:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cleanup(filepath.Join(cfg.StorageDir, id))
|
|
|
|
|
|
2023-03-01 19:48:49 +01:00
|
|
|
err = db.Delete(id)
|
2023-02-28 19:05:09 +01:00
|
|
|
if err != nil {
|
|
|
|
|
// non existent db entry with that id, or other db error, see logs
|
|
|
|
|
return fiber.NewError(404, "No upload with that id could be found!")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
2023-02-20 20:25:38 +01:00
|
|
|
}
|