Resolves MediaFire share IDs into direct links.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 

86 lines
2.0 KiB

package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/spf13/pflag"
)
func main() {
addr := pflag.StringP("addr", "a", ":8080", "The address to listen on")
help := pflag.Bool("help", false, "Show this help text")
envmap := map[string]string{
"addr": "MFDL_ADDR",
}
if val, ok := os.LookupEnv("PORT"); ok {
val = ":" + val
fmt.Printf("Setting --addr from PORT to %#v\n", val)
if err := pflag.Set("addr", val); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(2)
}
}
pflag.VisitAll(func(flag *pflag.Flag) {
if env, ok := envmap[flag.Name]; ok {
flag.Usage += fmt.Sprintf(" (env %s)", env)
if val, ok := os.LookupEnv(env); ok {
fmt.Printf("Setting --%s from %s to %#v\n", flag.Name, env, val)
if err := flag.Value.Set(val); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(2)
}
}
}
})
pflag.Parse()
if *help {
pflag.Usage()
os.Exit(1)
}
r := chi.NewRouter()
r.Use(middleware.Recoverer)
r.Use(middleware.NoCache)
r.Get("/", handleAPI)
r.Get("/s/{id}", handleLink)
fmt.Printf("Listening on http://%s\n", *addr)
if err := http.ListenAndServe(*addr, r); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func handleAPI(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{
"link_url": "/s/{id}",
})
}
func handleLink(w http.ResponseWriter, r *http.Request) {
l, err := getDirectLink(chi.URLParam(r, "id"))
switch {
case errFileStillUploading.Is(err):
http.Error(w, err.Error(), http.StatusServiceUnavailable)
case errNoSuchShare.Is(err):
http.Error(w, err.Error(), http.StatusNotFound)
case errUnknownMediaFireError.Is(err):
http.Error(w, err.Error(), http.StatusBadGateway)
case err != nil:
http.Error(w, err.Error(), http.StatusInternalServerError)
default:
http.Redirect(w, r, l, http.StatusTemporaryRedirect)
}
}