mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-12-25 23:36:30 +00:00
now Urlie will work around name collisions
This commit is contained in:
parent
c51e18e27e
commit
b5fe6fec95
1 changed files with 51 additions and 33 deletions
84
server.go
84
server.go
|
@ -522,21 +522,38 @@ func routeIPs(w http.ResponseWriter, r *http.Request) {
|
||||||
POST /urlie
|
POST /urlie
|
||||||
*/
|
*/
|
||||||
func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == "GET" {
|
||||||
|
err := UrliePage(w)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error: %s", err)
|
||||||
|
}
|
||||||
|
LogRequest(r, 200)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if r.Method == "POST" {
|
if r.Method == "POST" {
|
||||||
info := Info{
|
var (
|
||||||
|
err error
|
||||||
|
stored_filename string
|
||||||
|
local_filename string
|
||||||
|
useRandName bool = false
|
||||||
|
info Info
|
||||||
|
)
|
||||||
|
|
||||||
|
info = Info{
|
||||||
Ip: r.RemoteAddr,
|
Ip: r.RemoteAddr,
|
||||||
Random: hash.Rand64(),
|
Random: hash.Rand64(),
|
||||||
TimeStamp: time.Now(),
|
TimeStamp: time.Now(),
|
||||||
}
|
}
|
||||||
log.Println(info)
|
log.Println(info)
|
||||||
|
|
||||||
err := r.ParseMultipartForm(1024 * 5)
|
err = r.ParseMultipartForm(1024 * 5)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("%q", r.MultipartForm.Value)
|
log.Printf("%q", r.MultipartForm.Value)
|
||||||
var local_filename string
|
|
||||||
for k, v := range r.MultipartForm.Value {
|
for k, v := range r.MultipartForm.Value {
|
||||||
if k == "keywords" {
|
if k == "keywords" {
|
||||||
info.Keywords = append(info.Keywords, strings.Split(v[0], ",")...)
|
info.Keywords = append(info.Keywords, strings.Split(v[0], ",")...)
|
||||||
|
@ -551,56 +568,57 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Yay, hopefully we got an image!
|
// Yay, hopefully we got an image!
|
||||||
|
} else if k == "rand" {
|
||||||
|
useRandName = true
|
||||||
} else {
|
} else {
|
||||||
log.Printf("WARN: not sure what to do with param [%s = %s]", k, v)
|
log.Printf("WARN: not sure what to do with param [%s = %s]", k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exists, err := HasFileByFilename(local_filename)
|
exists, err := HasFileByFilename(filepath.Base(local_filename))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !exists {
|
|
||||||
file, err := gfs.Create(filepath.Base(local_filename))
|
|
||||||
defer file.Close()
|
|
||||||
if err != nil {
|
|
||||||
serverErr(w, r, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
local_fh, err := os.Open(local_filename)
|
if exists || useRandName {
|
||||||
defer local_fh.Close()
|
ext := filepath.Ext(local_filename)
|
||||||
if err != nil {
|
str := hash.GetSmallHash()
|
||||||
serverErr(w, r, err)
|
stored_filename = fmt.Sprintf("%s%s", str, ext)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
file.SetMeta(&info)
|
|
||||||
|
|
||||||
// copy the request body into the gfs file
|
|
||||||
n, err := io.Copy(file, local_fh)
|
|
||||||
if err != nil {
|
|
||||||
serverErr(w, r, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Printf("Wrote [%d] bytes from %s", n, local_filename)
|
|
||||||
|
|
||||||
http.Redirect(w, r, fmt.Sprintf("/v/%s", filepath.Base(local_filename)), 302)
|
|
||||||
} else {
|
} else {
|
||||||
|
stored_filename = filepath.Base(local_filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := gfs.Create(stored_filename)
|
||||||
|
defer file.Close()
|
||||||
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else if r.Method == "GET" {
|
|
||||||
err := UrliePage(w)
|
local_fh, err := os.Open(local_filename)
|
||||||
|
defer local_fh.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error: %s", err)
|
serverErr(w, r, err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file.SetMeta(&info)
|
||||||
|
|
||||||
|
// copy the request body into the gfs file
|
||||||
|
n, err := io.Copy(file, local_fh)
|
||||||
|
if err != nil {
|
||||||
|
serverErr(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("Wrote [%d] bytes from %s to %s", n, local_filename, stored_filename)
|
||||||
|
|
||||||
|
http.Redirect(w, r, fmt.Sprintf("/v/%s", stored_filename), 302)
|
||||||
} else {
|
} else {
|
||||||
LogRequest(r, 404)
|
LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
LogRequest(r, 200)
|
LogRequest(r, 200) // if we make it this far, then log success
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in a new issue