|
|
|
@@ -52,7 +52,7 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *Pages) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.Method != http.MethodPost && r.Method != http.MethodPut {
|
|
|
|
|
w.Header().Set("Allow", "POST,PUT")
|
|
|
|
|
http.Error(w, "invalid method", http.StatusMethodNotAllowed)
|
|
|
|
|
pages.HTTPError(w, r, "invalid method", http.StatusMethodNotAllowed)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -64,7 +64,7 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *Pages) http.HandlerFunc {
|
|
|
|
|
if r.Method == http.MethodPost || r.Header.Get("Content-Type") != "" {
|
|
|
|
|
mediaType, _, err = mime.ParseMediaType(r.Header.Get("Content-Type"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "failed to find or parse Content-Type", http.StatusBadRequest)
|
|
|
|
|
pages.HTTPError(w, r, "failed to find or parse Content-Type", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -84,11 +84,11 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *Pages) http.HandlerFunc {
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
if _, ok := errors.AsType[*http.MaxBytesError](err); ok {
|
|
|
|
|
http.Error(w, "upload too large", http.StatusRequestEntityTooLarge)
|
|
|
|
|
pages.HTTPError(w, r, "upload too large", http.StatusRequestEntityTooLarge)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http.Error(w, "invalid file", http.StatusBadRequest)
|
|
|
|
|
pages.HTTPError(w, r, "invalid file", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
@@ -96,11 +96,11 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *Pages) http.HandlerFunc {
|
|
|
|
|
input.MIMEType, err = detectMimeType(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("detect file mime type:", err)
|
|
|
|
|
http.Error(w, "failed to detect mime type", http.StatusInternalServerError)
|
|
|
|
|
pages.HTTPError(w, r, "failed to detect mime type", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if slices.Contains(cfg.BannedMIMETypes, input.MIMEType) {
|
|
|
|
|
http.Error(w, "content type not allowed", http.StatusBadRequest)
|
|
|
|
|
pages.HTTPError(w, r, "content type not allowed", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
input.Reader = file
|
|
|
|
@@ -110,7 +110,7 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *Pages) http.HandlerFunc {
|
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, cfg.MaxPasteBytes)
|
|
|
|
|
var content string
|
|
|
|
|
if mediaType == "application/x-www-form-urlencoded" {
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
|
err = r.ParseForm()
|
|
|
|
|
if err == nil {
|
|
|
|
|
content = r.PostForm.Get("content")
|
|
|
|
|
}
|
|
|
|
@@ -124,16 +124,16 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *Pages) http.HandlerFunc {
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
if _, ok := errors.AsType[*http.MaxBytesError](err); ok {
|
|
|
|
|
http.Error(w, "upload too large", http.StatusRequestEntityTooLarge)
|
|
|
|
|
pages.HTTPError(w, r, "upload too large", http.StatusRequestEntityTooLarge)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http.Error(w, "invalid paste upload", http.StatusBadRequest)
|
|
|
|
|
pages.HTTPError(w, r, "invalid paste upload", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.TrimSpace(content) == "" {
|
|
|
|
|
http.Error(w, "paste cannot be empty", http.StatusBadRequest)
|
|
|
|
|
pages.HTTPError(w, r, "paste cannot be empty", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -144,7 +144,7 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *Pages) http.HandlerFunc {
|
|
|
|
|
Type: ContentPaste,
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
http.Error(w, "unsupported Content-Type", http.StatusUnsupportedMediaType)
|
|
|
|
|
pages.HTTPError(w, r, "unsupported Content-Type", http.StatusUnsupportedMediaType)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -158,7 +158,7 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *Pages) http.HandlerFunc {
|
|
|
|
|
|
|
|
|
|
selected, found := findExpiry(cfg.Expiries, expLabel)
|
|
|
|
|
if !found {
|
|
|
|
|
http.Error(w, "invalid expiry", http.StatusBadRequest)
|
|
|
|
|
pages.HTTPError(w, r, "invalid expiry", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -174,14 +174,14 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *Pages) http.HandlerFunc {
|
|
|
|
|
path := getStoragePath(cfg.UploadDir, id)
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
|
|
|
|
log.Println("create directories:", err)
|
|
|
|
|
http.Error(w, "failed to save upload", http.StatusInternalServerError)
|
|
|
|
|
pages.HTTPError(w, r, "failed to save upload", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dest, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("create file:", err)
|
|
|
|
|
http.Error(w, "faield to save upload", http.StatusInternalServerError)
|
|
|
|
|
pages.HTTPError(w, r, "faield to save upload", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -199,23 +199,23 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *Pages) http.HandlerFunc {
|
|
|
|
|
size, err := io.Copy(io.MultiWriter(dest, hasher), input.Reader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if _, ok := errors.AsType[*http.MaxBytesError](err); ok {
|
|
|
|
|
http.Error(w, "upload too large", http.StatusRequestEntityTooLarge)
|
|
|
|
|
pages.HTTPError(w, r, "upload too large", http.StatusRequestEntityTooLarge)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
log.Println("write file:", err)
|
|
|
|
|
http.Error(w, "faield to write upload", http.StatusInternalServerError)
|
|
|
|
|
pages.HTTPError(w, r, "faield to write upload", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := dest.Close(); err != nil {
|
|
|
|
|
log.Println("close file:", err)
|
|
|
|
|
http.Error(w, "failed to save upload", http.StatusInternalServerError)
|
|
|
|
|
pages.HTTPError(w, r, "failed to save upload", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
createdAt := time.Now().UTC().Unix()
|
|
|
|
|
expires, err := selected.ExpiresFrom(time.Now().UTC())
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("calc expiry:", err) // im just using slang
|
|
|
|
|
http.Error(w, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
pages.HTTPError(w, r, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -250,7 +250,7 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *Pages) http.HandlerFunc {
|
|
|
|
|
err = insertUpload(db, &upload)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("save upload:", err)
|
|
|
|
|
http.Error(w, "failed to save upload", http.StatusInternalServerError)
|
|
|
|
|
pages.HTTPError(w, r, "failed to save upload", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
saved = true
|
|
|
|
@@ -280,12 +280,12 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *Pages) http.HandlerFunc {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func contentHandler(db *gorm.DB, cfg Config, tracker *AccessTracker) http.HandlerFunc {
|
|
|
|
|
func contentHandler(db *gorm.DB, cfg Config, tracker *AccessTracker, pages *Pages) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
id, _, _ := strings.Cut(r.PathValue("id"), ".") // remove any extension via strings.Cut
|
|
|
|
|
|
|
|
|
|
if !validPasteId(id) {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
pages.HTTPError(w, r, "upload not found", 404)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -293,28 +293,28 @@ func contentHandler(db *gorm.DB, cfg Config, tracker *AccessTracker) http.Handle
|
|
|
|
|
upload, err := getUploadById(db.WithContext(r.Context()), id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
pages.HTTPError(w, r, "upload not found", 404)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
log.Println("get upload:", err)
|
|
|
|
|
http.Error(w, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
pages.HTTPError(w, r, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
now := time.Now().UTC().Unix()
|
|
|
|
|
if upload.ExpiresAt != nil && now >= *upload.ExpiresAt {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
pages.HTTPError(w, r, "upload not found", 404)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file, err := os.Open(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
pages.HTTPError(w, r, "upload not found", 404)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
log.Println("get upload file:", err)
|
|
|
|
|
http.Error(w, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
pages.HTTPError(w, r, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -346,23 +346,23 @@ func contentHandler(db *gorm.DB, cfg Config, tracker *AccessTracker) http.Handle
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func contentInfoHandler(db *gorm.DB, tracker *AccessTracker) http.HandlerFunc {
|
|
|
|
|
func contentInfoHandler(db *gorm.DB, tracker *AccessTracker, pages *Pages) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
id, _, _ := strings.Cut(r.PathValue("id"), ".") // remove any extension via strings.Cut
|
|
|
|
|
|
|
|
|
|
if !validPasteId(id) {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
pages.HTTPError(w, r, "upload not found", 404)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
upload, err := getUploadById(db, id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
pages.HTTPError(w, r, "upload not found", 404)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
log.Println("get upload:", err)
|
|
|
|
|
http.Error(w, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
pages.HTTPError(w, r, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -378,11 +378,11 @@ func contentInfoHandler(db *gorm.DB, tracker *AccessTracker) http.HandlerFunc {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func deleteHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
|
|
|
|
|
func deleteHandler(db *gorm.DB, cfg Config, pages *Pages) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
id := r.PathValue("id")
|
|
|
|
|
if !validPasteId(id) {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
pages.HTTPError(w, r, "upload not found", 404)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
redirect := r.URL.Query().Get("redirect") == "true"
|
|
|
|
@@ -395,7 +395,7 @@ func deleteHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
|
|
|
|
|
var found bool
|
|
|
|
|
scheme, token, found = strings.Cut(bearer, " ")
|
|
|
|
|
if !found || scheme != "Bearer" {
|
|
|
|
|
http.Error(w, "invalid token", http.StatusBadRequest)
|
|
|
|
|
pages.HTTPError(w, r, "invalid token", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
case http.MethodPost:
|
|
|
|
@@ -406,33 +406,33 @@ func deleteHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
|
|
|
|
|
upload, err := getUploadById(db.WithContext(r.Context()), id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
pages.HTTPError(w, r, "upload not found", 404)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
log.Println("get upload:", err)
|
|
|
|
|
http.Error(w, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
pages.HTTPError(w, r, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if token == "" || subtle.ConstantTimeCompare([]byte(upload.DeleteToken), []byte(token)) != 1 {
|
|
|
|
|
http.Error(w, "invalid token", http.StatusBadRequest)
|
|
|
|
|
pages.HTTPError(w, r, "invalid token", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// continue to delete from database IF the file doesnt exist on disk
|
|
|
|
|
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
log.Println("remove upload file:", err)
|
|
|
|
|
http.Error(w, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
pages.HTTPError(w, r, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := db.WithContext(r.Context()).Delete(upload).Error; err != nil {
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
pages.HTTPError(w, r, "upload not found", 404)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Println("remove upload file:", err)
|
|
|
|
|
http.Error(w, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
pages.HTTPError(w, r, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -448,7 +448,7 @@ func deleteHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
|
|
|
|
|
func reportUploadHandler(db *gorm.DB, cfg Config, pages *Pages, cStore *ChallengeStore) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
|
http.Error(w, "Invalid form data", http.StatusBadRequest)
|
|
|
|
|
pages.HTTPError(w, r, "Invalid form data", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -485,7 +485,7 @@ func reportUploadHandler(db *gorm.DB, cfg Config, pages *Pages, cStore *Challeng
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err != nil && clientError {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
|
pages.HTTPError(w, r, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -503,7 +503,7 @@ func reportUploadHandler(db *gorm.DB, cfg Config, pages *Pages, cStore *Challeng
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Println("failed to insert report:", err)
|
|
|
|
|
http.Error(w, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
pages.HTTPError(w, r, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|