Skip to content

Commit

Permalink
Allow files in a bin to be listed in text/plain
Browse files Browse the repository at this point in the history
  • Loading branch information
espebra committed Sep 1, 2024
1 parent 1af4e87 commit a249a69
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (h *HTTP) Init() (err error) {
//h.router.HandleFunc("/admin/cleanup", h.Auth(h.ViewAdminCleanup)).Methods(http.MethodHead, http.MethodGet)
h.router.Handle("/static/{path:.*}", CacheControl(http.FileServer(http.FS(h.staticBox)))).Methods(http.MethodHead, http.MethodGet)
h.router.HandleFunc("/archive/{bin:[A-Za-z0-9_-]+}/{format:[a-z]+}", h.log(h.clientLookup(h.archive))).Methods(http.MethodHead, http.MethodGet)
h.router.HandleFunc("/{bin:[A-Za-z0-9_-]+}.txt", h.clientLookup(h.viewBinPlainText)).Methods(http.MethodHead, http.MethodGet)
h.router.HandleFunc("/qr/{bin:[A-Za-z0-9_-]+}", h.clientLookup(h.binQR)).Methods(http.MethodHead, http.MethodGet)
h.router.HandleFunc("/{bin:[A-Za-z0-9_-]+}/", h.clientLookup(h.viewBinRedirect)).Methods(http.MethodHead, http.MethodGet)
h.router.HandleFunc("/{bin:[A-Za-z0-9_-]+}", h.clientLookup(h.viewBin)).Methods(http.MethodHead, http.MethodGet)
Expand Down
57 changes: 57 additions & 0 deletions http_bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,63 @@ func (h *HTTP) viewBin(w http.ResponseWriter, r *http.Request) {
}
}

func (h *HTTP) viewBinPlainText(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=0")
w.Header().Set("X-Robots-Tag", "noindex")

params := mux.Vars(r)
inputBin := params["bin"]

type Data struct {
ds.Common
Bin ds.Bin `json:"bin"`
Files []ds.File `json:"files"`
}
var data Data

bin, found, err := h.dao.Bin().GetByID(inputBin)
if err != nil {
fmt.Printf("Unable to GetByID(%q): %s\n", inputBin, err.Error())
http.Error(w, "Errno 200", http.StatusInternalServerError)
return
}
if found {
files, err := h.dao.File().GetByBin(inputBin, true)
if err != nil {
fmt.Printf("Unable to GetByBin(%q): %s\n", inputBin, err.Error())
http.Error(w, "Not found", http.StatusNotFound)
return
}
if bin.IsReadable() {
data.Files = files
}
} else {
// Synthetize a bin without creating it. It will be created when a file is uploaded.
bin = ds.Bin{}
bin.Id = inputBin
bin.ExpiredAt = time.Now().UTC().Add(h.config.ExpirationDuration)
bin.ExpiredAtRelative = humanize.Time(bin.ExpiredAt)

// Intentional slowdown to make crawling less efficient
time.Sleep(1 * time.Second)
}

code := 200
if bin.IsReadable() == false {
code = 404
}

w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(code)
for _, file := range data.Files {
var u url.URL
u.Scheme = h.config.BaseUrl.Scheme
u.Host = h.config.BaseUrl.Host
u.Path = path.Join(h.config.BaseUrl.Path, file.URL)
fmt.Fprintf(w, "%s\n", u.String())
}
}

func (h *HTTP) binQR(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=31536000")
w.Header().Set("X-Robots-Tag", "noindex")
Expand Down
27 changes: 27 additions & 0 deletions http_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,33 @@ func TestBinInputValidation(t *testing.T) {
runTests(tcs, t)
}

func TestBinText(t *testing.T) {
tcs := []TestCase{
{
Description: "Create new bin",
Method: "POST",
Bin: "mytestbin6",
Filename: "a",
UploadContent: "content a",
StatusCode: 201,
}, {
Description: "Upload second file",
Method: "POST",
Bin: "mytestbin6",
Filename: "b",
UploadContent: "content b",
StatusCode: 201,
}, {
Description: "Get bin in plaintext",
Method: "GET",
Bin: "mytestbin6.txt",
DownloadContent: "/mytestbin6/a\n/mytestbin6/b\n",
StatusCode: 200,
},
}
runTests(tcs, t)
}

func TestBinBan(t *testing.T) {
tcs := []TestCase{
{
Expand Down

0 comments on commit a249a69

Please sign in to comment.