add a basic server for syncing
This commit is contained in:
parent
bd003dfa0b
commit
a3ad1a5d15
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
bin/
|
bin/
|
||||||
benchmark/
|
benchmark/
|
||||||
.gopack
|
.gopack
|
||||||
|
servertest
|
29
client/client.go
Normal file
29
client/client.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package gitmediaclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Send(filename string) error {
|
||||||
|
sha := filepath.Base(filename)
|
||||||
|
file, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("PUT", "http://localhost:8080/objects/"+sha, file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Sending %s from %s: %d\n", sha, filename, res.StatusCode)
|
||||||
|
return nil
|
||||||
|
}
|
19
cmd/git-media-server.go
Normal file
19
cmd/git-media-server.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
".."
|
||||||
|
"../server"
|
||||||
|
"fmt"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/objects/{oid}", gitmediaserver.GetObject).Methods("GET")
|
||||||
|
r.HandleFunc("/objects/{oid}", gitmediaserver.PutObject).Methods("PUT")
|
||||||
|
http.Handle("/", r)
|
||||||
|
fmt.Println("Serving out of", gitmedia.LocalMediaDir)
|
||||||
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
|
}
|
41
commands/command_sync.go
Normal file
41
commands/command_sync.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package gitmedia
|
||||||
|
|
||||||
|
import (
|
||||||
|
".."
|
||||||
|
"../client"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SyncCommand struct {
|
||||||
|
*Command
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *SyncCommand) Setup() {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *SyncCommand) Run() {
|
||||||
|
filepath.Walk(gitmedia.LocalMediaDir, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if info.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if ext := filepath.Ext(path); len(ext) > 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := gitmediaclient.Send(path); err != nil {
|
||||||
|
fmt.Println("Error uploading:", path)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
registerCommand("sync", func(c *Command) RunnableCommand {
|
||||||
|
return &SyncCommand{Command: c}
|
||||||
|
})
|
||||||
|
}
|
@ -22,7 +22,7 @@ func Clean(reader io.Reader) (*CleanedAsset, error) {
|
|||||||
|
|
||||||
sha1Hash := sha1.New()
|
sha1Hash := sha1.New()
|
||||||
writer := io.MultiWriter(sha1Hash, tmp)
|
writer := io.MultiWriter(sha1Hash, tmp)
|
||||||
io.Copy(writer, os.Stdin)
|
io.Copy(writer, reader)
|
||||||
|
|
||||||
return &CleanedAsset{tmp, hex.EncodeToString(sha1Hash.Sum(nil)), ""}, nil
|
return &CleanedAsset{tmp, hex.EncodeToString(sha1Hash.Sum(nil)), ""}, nil
|
||||||
}
|
}
|
||||||
|
31
gitmedia.go
31
gitmedia.go
@ -44,15 +44,32 @@ func LocalMediaPath(sha string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
wd, err := os.Getwd()
|
LocalMediaDir = resolveMediaDir()
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
LocalMediaDir = filepath.Join(wd, ".git", "media")
|
if err := os.MkdirAll(TempDir, 0744); err != nil {
|
||||||
|
|
||||||
if err = os.MkdirAll(TempDir, 0744); err != nil {
|
|
||||||
fmt.Printf("Error trying to create temp directory: %s\n", TempDir)
|
fmt.Printf("Error trying to create temp directory: %s\n", TempDir)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resolveMediaDir() string {
|
||||||
|
dir := os.Getenv("GIT_MEDIA_DIR")
|
||||||
|
if len(dir) > 0 {
|
||||||
|
return dir
|
||||||
|
}
|
||||||
|
|
||||||
|
dir = os.Getenv("GIT_DIR")
|
||||||
|
if len(dir) == 0 {
|
||||||
|
wd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
dir = wd
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolveGitDir(dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveGitDir(dir string) string {
|
||||||
|
return filepath.Join(dir, ".git", "media")
|
||||||
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
gofmt -w -l *.go \
|
gofmt -w -l *.go \
|
||||||
|
client/*.go \
|
||||||
cmd/*.go \
|
cmd/*.go \
|
||||||
commands/*.go \
|
commands/*.go \
|
||||||
filters/*.go \
|
filters/*.go \
|
||||||
script/*.go \
|
script/*.go \
|
||||||
|
server/*.go
|
||||||
|
56
server/server.go
Normal file
56
server/server.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package gitmediaserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
".."
|
||||||
|
"../filters"
|
||||||
|
"fmt"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PutObject(w http.ResponseWriter, r *http.Request) {
|
||||||
|
cleaned, err := gitmediafilters.Clean(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(500)
|
||||||
|
fmt.Fprint(w, err.Error())
|
||||||
|
}
|
||||||
|
defer cleaned.File.Close()
|
||||||
|
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
sha := vars["oid"]
|
||||||
|
if cleaned.Sha != sha {
|
||||||
|
w.WriteHeader(http.StatusConflict)
|
||||||
|
fmt.Fprint(w, "CONFLICT")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
localpath := gitmedia.LocalMediaPath(sha)
|
||||||
|
if stat, _ := os.Stat(localpath); stat != nil {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
fmt.Fprint(w, "OK")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = os.Rename(cleaned.File.Name(), localpath); err != nil {
|
||||||
|
w.WriteHeader(500)
|
||||||
|
fmt.Fprint(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
fmt.Fprint(w, "OK")
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetObject(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
sha := vars["oid"]
|
||||||
|
localpath := gitmedia.LocalMediaPath(sha)
|
||||||
|
file, err := os.Open(localpath)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(404)
|
||||||
|
fmt.Fprint(w, "nope nope nope")
|
||||||
|
}
|
||||||
|
|
||||||
|
io.Copy(w, file)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user