bluebell/upload/upload.go

206 lines
4.7 KiB
Go
Raw Normal View History

2023-07-22 00:59:45 +01:00
package upload
import (
"archive/tar"
"compress/gzip"
2024-08-16 16:48:50 +01:00
"context"
"crypto/subtle"
"errors"
2023-07-22 00:59:45 +01:00
"fmt"
2024-08-16 16:48:50 +01:00
"github.com/1f349/bluebell/database"
"github.com/dustin/go-humanize"
2023-07-22 00:59:45 +01:00
"github.com/julienschmidt/httprouter"
"github.com/spf13/afero"
"io"
2023-08-21 00:27:54 +01:00
"io/fs"
2023-07-22 00:59:45 +01:00
"net/http"
"path/filepath"
"slices"
2024-08-16 16:48:50 +01:00
"strings"
2023-07-22 00:59:45 +01:00
)
var indexBranches = []string{
"main",
"master",
}
2025-01-07 23:51:31 +00:00
func containsOnly(s string, f func(r rune) bool) bool {
for _, r := range []rune(s) {
if !f(r) {
return false
}
}
return true
}
func isValidSite(site string) bool {
if len(site) < 1 || site[0] == '-' {
return false
}
switch site[0] {
case '-':
return false
}
return containsOnly(site, func(r rune) bool {
return isAlphanumericOrDash(r) || r == '.'
})
}
func isValidBranch(branch string) bool {
if len(branch) < 1 {
return false
}
switch branch[0] {
case '-', '/':
return false
}
if branch[len(branch)-1] == '/' {
return false
}
return containsOnly(branch, func(r rune) bool {
return isAlphanumericOrDash(r) || r == '/' || r == '.'
})
}
func isAlphanumericOrDash(r rune) bool {
switch {
case r >= '0' && r <= '9':
return true
case r >= 'a' && r <= 'z':
return true
case r >= 'A' && r <= 'Z':
return true
case r == '-', r == '_':
return true
default:
return false
}
}
2024-08-16 16:48:50 +01:00
type sitesQueries interface {
GetSiteByDomain(ctx context.Context, domain string) (database.Site, error)
2023-07-22 00:59:45 +01:00
}
2024-08-16 16:48:50 +01:00
func New(storage afero.Fs, db sitesQueries) *Handler {
return &Handler{storage, db}
}
const maxFileSize = 1 * humanize.GiByte
2023-07-22 00:59:45 +01:00
type Handler struct {
storageFs afero.Fs
2024-08-16 16:48:50 +01:00
db sitesQueries
2023-07-22 00:59:45 +01:00
}
func (h *Handler) Handle(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
site := params.ByName("site")
branch := params.ByName("branch")
2023-08-21 00:27:54 +01:00
siteConf, err := h.db.GetSiteByDomain(req.Context(), site)
2024-08-16 16:48:50 +01:00
if err != nil {
http.Error(rw, "", http.StatusNotFound)
2023-08-21 00:27:54 +01:00
return
}
token, ok := strings.CutPrefix(req.Header.Get("Authorization"), "Bearer ")
if !ok || subtle.ConstantTimeCompare([]byte(token), []byte(siteConf.Token)) == 0 {
2023-08-21 00:27:54 +01:00
http.Error(rw, "403 Forbidden", http.StatusForbidden)
return
}
2023-07-22 00:59:45 +01:00
fileData, fileHeader, err := req.FormFile("upload")
if err != nil {
http.Error(rw, "Missing file upload", http.StatusBadRequest)
return
}
// if file is bigger than maxFileSize
2024-08-16 16:48:50 +01:00
if fileHeader.Size > maxFileSize {
http.Error(rw, "File too big", http.StatusInsufficientStorage)
2023-07-22 00:59:45 +01:00
return
}
err = h.extractTarGzUpload(fileData, site, branch)
if err != nil {
http.Error(rw, fmt.Sprintf("Invalid upload: %s", err), http.StatusBadRequest)
return
}
rw.WriteHeader(http.StatusAccepted)
}
func (h *Handler) extractTarGzUpload(fileData io.Reader, site, branch string) error {
2025-01-07 23:51:31 +00:00
if !isValidSite(site) {
return fmt.Errorf("invalid site name: %s", site)
}
if !isValidBranch(branch) {
return fmt.Errorf("invalid branch name: %s", branch)
}
if slices.Contains(indexBranches, branch) {
branch = ""
}
2025-01-07 23:51:31 +00:00
_, err := h.db.GetSiteByDomain(context.Background(), site)
if err != nil {
return fmt.Errorf("invalid site: %w", err)
}
siteBranchPath := filepath.Join(site, "@"+branch)
2025-01-07 21:26:22 +00:00
siteBranchOldPath := filepath.Join(site, "old@"+branch)
2025-01-07 21:18:37 +00:00
// try the new "old@[...]" and old "@[...].old" paths
2025-01-07 23:51:31 +00:00
err = h.storageFs.RemoveAll(siteBranchPath + ".old")
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("failed to remove old site branch %s: %w", siteBranchPath, err)
}
2025-01-07 21:26:22 +00:00
err = h.storageFs.RemoveAll(siteBranchOldPath)
2025-01-07 21:18:37 +00:00
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("failed to remove old site branch %s: %w", siteBranchPath, err)
}
2025-01-07 21:26:22 +00:00
err = h.storageFs.Rename(siteBranchPath, siteBranchOldPath)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
2023-08-21 00:27:54 +01:00
return fmt.Errorf("failed to save an old copy of the site: %w", err)
}
err = h.storageFs.MkdirAll(siteBranchPath, fs.ModePerm)
if err != nil {
return fmt.Errorf("failed to make site directory: %w", err)
}
branchFs := afero.NewBasePathFs(h.storageFs, siteBranchPath)
2023-07-22 00:59:45 +01:00
// decompress gzip wrapper
gzipReader, err := gzip.NewReader(fileData)
if err != nil {
return fmt.Errorf("invalid gzip file: %w", err)
}
// parse tar encoding
tarReader := tar.NewReader(gzipReader)
for {
next, err := tarReader.Next()
if err == io.EOF {
2023-08-21 00:27:54 +01:00
// finished reading tar, exit now
2023-07-22 00:59:45 +01:00
break
}
if err != nil {
return fmt.Errorf("invalid tar archive: %w", err)
}
2023-08-21 00:27:54 +01:00
err = branchFs.MkdirAll(filepath.Dir(next.Name), fs.ModePerm)
2023-07-22 00:59:45 +01:00
if err != nil {
return fmt.Errorf("failed to make directory tree: %w", err)
}
2023-08-21 00:27:54 +01:00
create, err := branchFs.Create(next.Name)
2023-07-22 00:59:45 +01:00
if err != nil {
return fmt.Errorf("failed to create output file: '%s': %w", next.Name, err)
}
_, err = io.Copy(create, tarReader)
if err != nil {
return fmt.Errorf("failed to copy from archive to output file: '%s': %w", next.Name, err)
}
}
return nil
}