Skip to content

Commit

Permalink
Release 1.1.0: Added package upload
Browse files Browse the repository at this point in the history
  • Loading branch information
c4s4 committed Jul 28, 2015
2 parents ea10189 + 43a2961 commit e34b0b4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Semantic changelog: https://github.com/c4s4/changelog

- version: 1.1.0
date: 2015-07-28
summary: Added package upload
added:
- "Package upload without authentication."

- version: 1.0.0
date: 2015-07-28
summary: First release
Expand Down
56 changes: 46 additions & 10 deletions cheeseshop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -72,17 +73,52 @@ func servePackage(dir, file string, w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filename)
}

func handler(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path[len(*path):], "/")
if len(parts) > 2 {
http.Error(w, fmt.Sprintf("%s is not a valid path", r.URL.Path), 404)
func copyFile(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(100000)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else if len(parts) == 1 && parts[0] == "" {
listRoot(w, r)
} else if len(parts) == 1 {
listDirectory(parts[0], w, r)
} else {
servePackage(parts[0], parts[1], w, r)
}
m := r.MultipartForm
files := m.File["content"]
for _, file := range files {
name := file.Filename
pack := name[:strings.LastIndex(name, "-")]
log.Printf("Writing file %s", name)
f, err := file.Open()
defer f.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
dst, err := os.Create(filepath.Join(*root, pack, name))
defer dst.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := io.Copy(dst, f); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}

func handler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
parts := strings.Split(r.URL.Path[len(*path):], "/")
if len(parts) > 2 {
http.Error(w, fmt.Sprintf("%s is not a valid path", r.URL.Path), 404)
return
} else if len(parts) == 1 && parts[0] == "" {
listRoot(w, r)
} else if len(parts) == 1 {
listDirectory(parts[0], w, r)
} else {
servePackage(parts[0], parts[1], w, r)
}
} else if r.Method == "POST" {
copyFile(w, r)
}
}

Expand Down

0 comments on commit e34b0b4

Please sign in to comment.