mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-24 00:55:41 +00:00
running go fmt
This commit is contained in:
parent
7ba7c07041
commit
9a5ded48a6
10 changed files with 906 additions and 925 deletions
21
client.go
21
client.go
|
@ -15,15 +15,15 @@ import (
|
|||
func PutFileFromPath(host, filename string) (path string, err error) {
|
||||
ext := filepath.Ext(filename)
|
||||
file, err := os.Open(filename)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
resp, err := http.Post(host, mime.TypeByExtension(ext) , bufio.NewReader(file))
|
||||
if (err != nil) {
|
||||
resp, err := http.Post(host, mime.TypeByExtension(ext), bufio.NewReader(file))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
bytes, err := ioutil.ReadAll(resp.Body)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return string(bytes), nil
|
||||
|
@ -33,7 +33,7 @@ func FetchFileFromURL(url string) (filename string, err error) {
|
|||
var t time.Time
|
||||
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{ InsecureSkipVerify: true },
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
client := &http.Client{
|
||||
//CheckRedirect: redirectPolicyFunc,
|
||||
|
@ -41,14 +41,14 @@ func FetchFileFromURL(url string) (filename string, err error) {
|
|||
}
|
||||
resp, err := client.Get(url)
|
||||
defer resp.Body.Close()
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
mtime := resp.Header.Get("last-modified")
|
||||
if (len(mtime) > 0) {
|
||||
if len(mtime) > 0 {
|
||||
t, err = time.Parse(http.TimeFormat, mtime)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
|
@ -60,11 +60,11 @@ func FetchFileFromURL(url string) (filename string, err error) {
|
|||
log.Println(resp)
|
||||
|
||||
bytes, err := ioutil.ReadAll(resp.Body)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(os.TempDir(), url_filename), bytes, 0644)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = os.Chtimes(filepath.Join(os.TempDir(), url_filename), t, t)
|
||||
|
@ -72,4 +72,3 @@ func FetchFileFromURL(url string) (filename string, err error) {
|
|||
// lastly, return
|
||||
return filepath.Join(os.TempDir(), url_filename), nil
|
||||
}
|
||||
|
||||
|
|
13
config.go
13
config.go
|
@ -1,8 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"launchpad.net/goyaml"
|
||||
"io/ioutil"
|
||||
"launchpad.net/goyaml"
|
||||
)
|
||||
|
||||
type Config map[string]interface{}
|
||||
|
@ -11,8 +11,10 @@ func (c *Config) GetBool(option string) (value bool) {
|
|||
conf := Config{}
|
||||
conf = *c
|
||||
switch conf[option] {
|
||||
default: value = false
|
||||
case "yes", "on", "true": value = true
|
||||
default:
|
||||
value = false
|
||||
case "yes", "on", "true":
|
||||
value = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -26,12 +28,12 @@ func (c *Config) GetString(option string) (value string) {
|
|||
|
||||
func ReadConfigFile(filename string) (config Config, err error) {
|
||||
bytes, err := ioutil.ReadFile(filename)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = goyaml.Unmarshal(bytes, &config)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -41,4 +43,3 @@ func ReadConfigFile(filename string) (config Config, err error) {
|
|||
func WriteConfigFile(filename string, data []byte) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -8,8 +7,8 @@ import (
|
|||
/* gfs is a *mgo.GridFS defined in imgsrv.go */
|
||||
|
||||
func GetFileByFilename(filename string) (this_file File, err error) {
|
||||
err = gfs.Find(bson.M{"filename":filename}).One(&this_file)
|
||||
if (err != nil) {
|
||||
err = gfs.Find(bson.M{"filename": filename}).One(&this_file)
|
||||
if err != nil {
|
||||
return this_file, err
|
||||
}
|
||||
return this_file, nil
|
||||
|
@ -17,14 +16,14 @@ func GetFileByFilename(filename string) (this_file File, err error) {
|
|||
|
||||
func GetFileRandom() (this_file File, err error) {
|
||||
r := Rand64()
|
||||
err = gfs.Find(bson.M{"random": bson.M{"$gt" : r } }).One(&this_file)
|
||||
if (err != nil) {
|
||||
err = gfs.Find(bson.M{"random": bson.M{"$gt": r}}).One(&this_file)
|
||||
if err != nil {
|
||||
return this_file, err
|
||||
}
|
||||
if (len(this_file.Md5) == 0) {
|
||||
err = gfs.Find(bson.M{"random": bson.M{"$lt" : r } }).One(&this_file)
|
||||
if len(this_file.Md5) == 0 {
|
||||
err = gfs.Find(bson.M{"random": bson.M{"$lt": r}}).One(&this_file)
|
||||
}
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return this_file, err
|
||||
}
|
||||
return this_file, nil
|
||||
|
@ -33,7 +32,7 @@ func GetFileRandom() (this_file File, err error) {
|
|||
/* Check whether this File filename is on Mongo */
|
||||
func HasFileByFilename(filename string) (exists bool, err error) {
|
||||
c, err := gfs.Find(bson.M{"filename": filename}).Count()
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
exists = (c > 0)
|
||||
|
@ -41,8 +40,8 @@ func HasFileByFilename(filename string) (exists bool, err error) {
|
|||
}
|
||||
|
||||
func HasFileByMd5(md5 string) (exists bool, err error) {
|
||||
c, err := gfs.Find(bson.M{"md5": md5 }).Count()
|
||||
if (err != nil) {
|
||||
c, err := gfs.Find(bson.M{"md5": md5}).Count()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
exists = (c > 0)
|
||||
|
@ -50,11 +49,10 @@ func HasFileByMd5(md5 string) (exists bool, err error) {
|
|||
}
|
||||
|
||||
func HasFileByKeyword(keyword string) (exists bool, err error) {
|
||||
c, err := gfs.Find(bson.M{"metadata": bson.M{"keywords": keyword} }).Count()
|
||||
if (err != nil) {
|
||||
c, err := gfs.Find(bson.M{"metadata": bson.M{"keywords": keyword}}).Count()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
exists = (c > 0)
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
|
|
3
hash.go
3
hash.go
|
@ -17,7 +17,7 @@ func Rand64() int64 {
|
|||
func GetMd5FromString(blob string) (sum []byte) {
|
||||
h := md5.New()
|
||||
defer h.Reset()
|
||||
io.WriteString(h,blob)
|
||||
io.WriteString(h, blob)
|
||||
return h.Sum(nil)
|
||||
}
|
||||
|
||||
|
@ -35,4 +35,3 @@ func GetSmallHash() (small_hash string) {
|
|||
io.WriteString(h, fmt.Sprintf("%d", time.Now().Unix()))
|
||||
return fmt.Sprintf("%X", h.Sum(nil))
|
||||
}
|
||||
|
||||
|
|
13
hash_test.go
13
hash_test.go
|
@ -9,10 +9,10 @@ func TestRand64(t *testing.T) {
|
|||
var i interface{}
|
||||
i = Rand64()
|
||||
v, ok := i.(int64)
|
||||
if (!ok) {
|
||||
if !ok {
|
||||
t.Errorf("Rand64 returned wrong type")
|
||||
}
|
||||
if (v < 0) {
|
||||
if v < 0 {
|
||||
t.Errorf("Rand64 returned a too small number [%d]", v)
|
||||
}
|
||||
}
|
||||
|
@ -21,8 +21,8 @@ func TestMd5Bytes(t *testing.T) {
|
|||
var blob = []byte("Hurp til you Derp")
|
||||
var expected = "3ef08fa896a154eee3c97f037c9d6dfc"
|
||||
var actual = fmt.Sprintf("%x", GetMd5FromBytes(blob))
|
||||
if (actual != expected) {
|
||||
t.Errorf("Md5FromBytes sum did not match! %s != %s",actual,expected)
|
||||
if actual != expected {
|
||||
t.Errorf("Md5FromBytes sum did not match! %s != %s", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,11 +30,10 @@ func TestMd5String(t *testing.T) {
|
|||
var blob = "Hurp til you Derp"
|
||||
var expected = "3ef08fa896a154eee3c97f037c9d6dfc"
|
||||
var actual = fmt.Sprintf("%x", GetMd5FromString(blob))
|
||||
if (actual != expected) {
|
||||
t.Errorf("Md5FromString sum did not match! %s != %s",actual,expected)
|
||||
if actual != expected {
|
||||
t.Errorf("Md5FromString sum did not match! %s != %s", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHash(t *testing.T) {
|
||||
}
|
||||
|
||||
|
|
39
imgsrv.go
39
imgsrv.go
|
@ -58,40 +58,40 @@ func main() {
|
|||
// to override variables
|
||||
loadConfiguration(ConfigFile)
|
||||
|
||||
if (len(FetchUrl) > 0) {
|
||||
if len(FetchUrl) > 0 {
|
||||
file, err := FetchFileFromURL(FetchUrl)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
log.Println(file)
|
||||
} else if (RunAsServer) {
|
||||
} else if RunAsServer {
|
||||
log.Printf("%s", ServerIP)
|
||||
runServer(ServerIP,ServerPort)
|
||||
runServer(ServerIP, ServerPort)
|
||||
} else {
|
||||
if (len(RemoteHost) == 0) {
|
||||
if len(RemoteHost) == 0 {
|
||||
log.Println("Please provide a remotehost!")
|
||||
return
|
||||
}
|
||||
if (len(PutFile) == 0 ) { //&& len(flag.Args()) == 0) {
|
||||
if len(PutFile) == 0 { //&& len(flag.Args()) == 0) {
|
||||
log.Println("Please provide files to be uploaded!")
|
||||
return
|
||||
}
|
||||
_,basename := filepath.Split(PutFile)
|
||||
_, basename := filepath.Split(PutFile)
|
||||
queryParams := "?filename=" + basename
|
||||
if (len(FileKeywords) > 0) {
|
||||
if len(FileKeywords) > 0 {
|
||||
queryParams = queryParams + "&keywords=" + FileKeywords
|
||||
} else {
|
||||
log.Println("WARN: you didn't provide any keywords :-(")
|
||||
}
|
||||
url, err := url.Parse(RemoteHost + "/f/" + queryParams)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
log.Printf("POSTing: %s\n", url.String())
|
||||
url_path, err := PutFileFromPath(url.String(), PutFile)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
@ -162,7 +162,7 @@ func init() {
|
|||
func loadConfiguration(filename string) (c Config) {
|
||||
//log.Printf("Attempting to load config file: %s", filename)
|
||||
c, err := ReadConfigFile(filename)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
//log.Println(err)
|
||||
return Config{}
|
||||
}
|
||||
|
@ -178,31 +178,30 @@ func loadConfiguration(filename string) (c Config) {
|
|||
|
||||
// Only set variables from config file,
|
||||
// if they weren't passed as flags
|
||||
if (DefaultRunAsServer == RunAsServer && cRunAsServer) {
|
||||
if DefaultRunAsServer == RunAsServer && cRunAsServer {
|
||||
RunAsServer = cRunAsServer
|
||||
}
|
||||
if (DefaultServerIP == ServerIP && len(cServerIp) > 0) {
|
||||
if DefaultServerIP == ServerIP && len(cServerIp) > 0 {
|
||||
ServerIP = cServerIp
|
||||
}
|
||||
if (DefaultServerPort == ServerPort && len(cServerPort) > 0) {
|
||||
if DefaultServerPort == ServerPort && len(cServerPort) > 0 {
|
||||
ServerPort = cServerPort
|
||||
}
|
||||
if (DefaultMongoHost == MongoHost && len(cMongoHost) > 0) {
|
||||
if DefaultMongoHost == MongoHost && len(cMongoHost) > 0 {
|
||||
MongoHost = cMongoHost
|
||||
}
|
||||
if (DefaultMongoDB == MongoDB && len(cMongoDB) > 0) {
|
||||
if DefaultMongoDB == MongoDB && len(cMongoDB) > 0 {
|
||||
MongoDB = cMongoDB
|
||||
}
|
||||
if (DefaultMongoUsername == MongoUsername && len(cMongoUsername) > 0) {
|
||||
if DefaultMongoUsername == MongoUsername && len(cMongoUsername) > 0 {
|
||||
MongoUsername = cMongoUsername
|
||||
}
|
||||
if (DefaultMongoPassword == MongoPassword && len(cMongoPassword) > 0) {
|
||||
if DefaultMongoPassword == MongoPassword && len(cMongoPassword) > 0 {
|
||||
MongoPassword = cMongoPassword
|
||||
}
|
||||
if (DefaultRemoteHost == RemoteHost && len(cRemoteHost) > 0) {
|
||||
if DefaultRemoteHost == RemoteHost && len(cRemoteHost) > 0 {
|
||||
RemoteHost = cRemoteHost
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
|
|
55
layouts.go
55
layouts.go
|
@ -8,7 +8,6 @@ import (
|
|||
|
||||
var emptyInterface interface{}
|
||||
|
||||
|
||||
var headTemplate = template.Must(template.New("head").Parse(headTemplateHTML))
|
||||
var headTemplateHTML = `
|
||||
<html>
|
||||
|
@ -128,86 +127,76 @@ var listTemplateHTML = `
|
|||
`
|
||||
|
||||
func UrliePage(w io.Writer) (err error) {
|
||||
err = headTemplate.Execute(w, map[string]string{"title" : "FileSrv :: URLie"})
|
||||
if (err != nil) {
|
||||
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: URLie"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = navbarTemplate.Execute(w, nil)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = containerBeginTemplate.Execute(w, nil)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = formGetUrlTemplate.Execute(w, &emptyInterface)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = tailTemplate.Execute(w, map[string]string{"footer" : fmt.Sprintf("Version: %s", VERSION)})
|
||||
if (err != nil) {
|
||||
err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return
|
||||
}
|
||||
func UploadPage(w io.Writer) (err error) {
|
||||
err = headTemplate.Execute(w, map[string]string{"title" : "FileSrv :: Upload"})
|
||||
if (err != nil) {
|
||||
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: Upload"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = navbarTemplate.Execute(w, nil)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = containerBeginTemplate.Execute(w, nil)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = formFileUploadTemplate.Execute(w, &emptyInterface)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = tailTemplate.Execute(w, map[string]string{"footer" : fmt.Sprintf("Version: %s", VERSION)})
|
||||
if (err != nil) {
|
||||
err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ListFilesPage(w io.Writer, files []File) (err error) {
|
||||
err = headTemplate.Execute(w, map[string]string{"title" : "FileSrv"})
|
||||
if (err != nil) {
|
||||
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = navbarTemplate.Execute(w, nil)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = containerBeginTemplate.Execute(w, nil)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = listTemplate.Execute(w, files)
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = tailTemplate.Execute(w, map[string]string{"footer" : fmt.Sprintf("Version: %s", VERSION)})
|
||||
if (err != nil) {
|
||||
err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
var bootstrapCSS = `
|
||||
/*!
|
||||
* Bootstrap v2.2.2
|
||||
|
@ -6249,5 +6238,3 @@ a.badge:hover {
|
|||
position: fixed;
|
||||
}
|
||||
`
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue