~alazarte/uploader

321775defa6a3e3be0615e5c831ebe862c02a9f7 — alazarte a month ago 054f87e
Allow to paste text files
1 files changed, 20 insertions(+), 6 deletions(-)

M main.go
M main.go => main.go +20 -6
@@ 5,6 5,7 @@ import (
	"flag"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"os"
	"path/filepath"


@@ 23,12 24,21 @@ var (
	secretCurrent  string
	secretIndex    int
	rot            rotator

	abc = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghiJKLMNOPQrstuvwxyz01"
)

type rotator struct {
	rotate func()
}

func tenLetterName() (s string) {
	for i := 0; i < 10; i++ {
		s = fmt.Sprintf("%s%s", s, string(abc[rand.Int()%100]))
	}
	return
}

func upload(w http.ResponseWriter, r *http.Request) {
	// upload only 10 Mb
	if err := r.ParseMultipartForm(10 << 20); err != nil {


@@ 43,14 53,18 @@ func upload(w http.ResponseWriter, r *http.Request) {
		return
	}

	files, ok := r.MultipartForm.File["file"]
	if !ok {
		log.Println("no file found")
		w.WriteHeader(http.StatusBadRequest)
		return
	paste, ok := r.MultipartForm.Value["paste"]
	if ok {
		name := fmt.Sprintf("%s/%s", outputPath, tenLetterName())
		err := os.WriteFile(name, []byte(paste[0]), 0644)
		if err != nil {
			log.Println("error writing file: ", err)
			w.WriteHeader(http.StatusInternalServerError)
		}
	}

	log.Println(len(files), "files found")
	files, ok := r.MultipartForm.File["file"]
	log.Println(len(files), "attached files found")
	for _, v := range files {
		f, err := v.Open()
		if err != nil {