1
0
Fork 0
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:
Vincent Batts 2013-03-27 16:30:24 -04:00
parent 7ba7c07041
commit 9a5ded48a6
10 changed files with 906 additions and 925 deletions

View file

@ -15,15 +15,15 @@ import (
func PutFileFromPath(host, filename string) (path string, err error) { func PutFileFromPath(host, filename string) (path string, err error) {
ext := filepath.Ext(filename) ext := filepath.Ext(filename)
file, err := os.Open(filename) file, err := os.Open(filename)
if (err != nil) { if err != nil {
return return
} }
resp, err := http.Post(host, mime.TypeByExtension(ext) , bufio.NewReader(file)) resp, err := http.Post(host, mime.TypeByExtension(ext), bufio.NewReader(file))
if (err != nil) { if err != nil {
return return
} }
bytes, err := ioutil.ReadAll(resp.Body) bytes, err := ioutil.ReadAll(resp.Body)
if (err != nil) { if err != nil {
return return
} }
return string(bytes), nil return string(bytes), nil
@ -33,7 +33,7 @@ func FetchFileFromURL(url string) (filename string, err error) {
var t time.Time var t time.Time
tr := &http.Transport{ tr := &http.Transport{
TLSClientConfig: &tls.Config{ InsecureSkipVerify: true }, TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
} }
client := &http.Client{ client := &http.Client{
//CheckRedirect: redirectPolicyFunc, //CheckRedirect: redirectPolicyFunc,
@ -41,14 +41,14 @@ func FetchFileFromURL(url string) (filename string, err error) {
} }
resp, err := client.Get(url) resp, err := client.Get(url)
defer resp.Body.Close() defer resp.Body.Close()
if (err != nil) { if err != nil {
return return
} }
mtime := resp.Header.Get("last-modified") mtime := resp.Header.Get("last-modified")
if (len(mtime) > 0) { if len(mtime) > 0 {
t, err = time.Parse(http.TimeFormat, mtime) t, err = time.Parse(http.TimeFormat, mtime)
if (err != nil) { if err != nil {
return return
} }
} else { } else {
@ -60,11 +60,11 @@ func FetchFileFromURL(url string) (filename string, err error) {
log.Println(resp) log.Println(resp)
bytes, err := ioutil.ReadAll(resp.Body) bytes, err := ioutil.ReadAll(resp.Body)
if (err != nil) { if err != nil {
return return
} }
err = ioutil.WriteFile(filepath.Join(os.TempDir(), url_filename), bytes, 0644) err = ioutil.WriteFile(filepath.Join(os.TempDir(), url_filename), bytes, 0644)
if (err != nil) { if err != nil {
return return
} }
err = os.Chtimes(filepath.Join(os.TempDir(), url_filename), t, t) 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 // lastly, return
return filepath.Join(os.TempDir(), url_filename), nil return filepath.Join(os.TempDir(), url_filename), nil
} }

View file

@ -1,8 +1,8 @@
package main package main
import ( import (
"launchpad.net/goyaml"
"io/ioutil" "io/ioutil"
"launchpad.net/goyaml"
) )
type Config map[string]interface{} type Config map[string]interface{}
@ -11,8 +11,10 @@ func (c *Config) GetBool(option string) (value bool) {
conf := Config{} conf := Config{}
conf = *c conf = *c
switch conf[option] { switch conf[option] {
default: value = false default:
case "yes", "on", "true": value = true value = false
case "yes", "on", "true":
value = true
} }
return return
} }
@ -26,12 +28,12 @@ func (c *Config) GetString(option string) (value string) {
func ReadConfigFile(filename string) (config Config, err error) { func ReadConfigFile(filename string) (config Config, err error) {
bytes, err := ioutil.ReadFile(filename) bytes, err := ioutil.ReadFile(filename)
if (err != nil) { if err != nil {
return return
} }
err = goyaml.Unmarshal(bytes, &config) err = goyaml.Unmarshal(bytes, &config)
if (err != nil) { if err != nil {
return return
} }
@ -41,4 +43,3 @@ func ReadConfigFile(filename string) (config Config, err error) {
func WriteConfigFile(filename string, data []byte) (err error) { func WriteConfigFile(filename string, data []byte) (err error) {
return return
} }

View file

@ -1,4 +1,3 @@
package main package main
import ( import (
@ -8,8 +7,8 @@ import (
/* gfs is a *mgo.GridFS defined in imgsrv.go */ /* gfs is a *mgo.GridFS defined in imgsrv.go */
func GetFileByFilename(filename string) (this_file File, err error) { func GetFileByFilename(filename string) (this_file File, err error) {
err = gfs.Find(bson.M{"filename":filename}).One(&this_file) err = gfs.Find(bson.M{"filename": filename}).One(&this_file)
if (err != nil) { if err != nil {
return this_file, err return this_file, err
} }
return this_file, nil return this_file, nil
@ -17,14 +16,14 @@ func GetFileByFilename(filename string) (this_file File, err error) {
func GetFileRandom() (this_file File, err error) { func GetFileRandom() (this_file File, err error) {
r := Rand64() r := Rand64()
err = gfs.Find(bson.M{"random": bson.M{"$gt" : r } }).One(&this_file) err = gfs.Find(bson.M{"random": bson.M{"$gt": r}}).One(&this_file)
if (err != nil) { if err != nil {
return this_file, err return this_file, err
} }
if (len(this_file.Md5) == 0) { if len(this_file.Md5) == 0 {
err = gfs.Find(bson.M{"random": bson.M{"$lt" : r } }).One(&this_file) 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, err
} }
return this_file, nil return this_file, nil
@ -33,7 +32,7 @@ func GetFileRandom() (this_file File, err error) {
/* Check whether this File filename is on Mongo */ /* Check whether this File filename is on Mongo */
func HasFileByFilename(filename string) (exists bool, err error) { func HasFileByFilename(filename string) (exists bool, err error) {
c, err := gfs.Find(bson.M{"filename": filename}).Count() c, err := gfs.Find(bson.M{"filename": filename}).Count()
if (err != nil) { if err != nil {
return false, err return false, err
} }
exists = (c > 0) exists = (c > 0)
@ -41,8 +40,8 @@ func HasFileByFilename(filename string) (exists bool, err error) {
} }
func HasFileByMd5(md5 string) (exists bool, err error) { func HasFileByMd5(md5 string) (exists bool, err error) {
c, err := gfs.Find(bson.M{"md5": md5 }).Count() c, err := gfs.Find(bson.M{"md5": md5}).Count()
if (err != nil) { if err != nil {
return false, err return false, err
} }
exists = (c > 0) exists = (c > 0)
@ -50,11 +49,10 @@ func HasFileByMd5(md5 string) (exists bool, err error) {
} }
func HasFileByKeyword(keyword 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() c, err := gfs.Find(bson.M{"metadata": bson.M{"keywords": keyword}}).Count()
if (err != nil) { if err != nil {
return false, err return false, err
} }
exists = (c > 0) exists = (c > 0)
return exists, nil return exists, nil
} }

View file

@ -17,7 +17,7 @@ func Rand64() int64 {
func GetMd5FromString(blob string) (sum []byte) { func GetMd5FromString(blob string) (sum []byte) {
h := md5.New() h := md5.New()
defer h.Reset() defer h.Reset()
io.WriteString(h,blob) io.WriteString(h, blob)
return h.Sum(nil) return h.Sum(nil)
} }
@ -35,4 +35,3 @@ func GetSmallHash() (small_hash string) {
io.WriteString(h, fmt.Sprintf("%d", time.Now().Unix())) io.WriteString(h, fmt.Sprintf("%d", time.Now().Unix()))
return fmt.Sprintf("%X", h.Sum(nil)) return fmt.Sprintf("%X", h.Sum(nil))
} }

View file

@ -9,10 +9,10 @@ func TestRand64(t *testing.T) {
var i interface{} var i interface{}
i = Rand64() i = Rand64()
v, ok := i.(int64) v, ok := i.(int64)
if (!ok) { if !ok {
t.Errorf("Rand64 returned wrong type") t.Errorf("Rand64 returned wrong type")
} }
if (v < 0) { if v < 0 {
t.Errorf("Rand64 returned a too small number [%d]", v) 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 blob = []byte("Hurp til you Derp")
var expected = "3ef08fa896a154eee3c97f037c9d6dfc" var expected = "3ef08fa896a154eee3c97f037c9d6dfc"
var actual = fmt.Sprintf("%x", GetMd5FromBytes(blob)) var actual = fmt.Sprintf("%x", GetMd5FromBytes(blob))
if (actual != expected) { if actual != expected {
t.Errorf("Md5FromBytes sum did not match! %s != %s",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 blob = "Hurp til you Derp"
var expected = "3ef08fa896a154eee3c97f037c9d6dfc" var expected = "3ef08fa896a154eee3c97f037c9d6dfc"
var actual = fmt.Sprintf("%x", GetMd5FromString(blob)) var actual = fmt.Sprintf("%x", GetMd5FromString(blob))
if (actual != expected) { if actual != expected {
t.Errorf("Md5FromString sum did not match! %s != %s",actual,expected) t.Errorf("Md5FromString sum did not match! %s != %s", actual, expected)
} }
} }
func TestHash(t *testing.T) { func TestHash(t *testing.T) {
} }

View file

@ -58,40 +58,40 @@ func main() {
// to override variables // to override variables
loadConfiguration(ConfigFile) loadConfiguration(ConfigFile)
if (len(FetchUrl) > 0) { if len(FetchUrl) > 0 {
file, err := FetchFileFromURL(FetchUrl) file, err := FetchFileFromURL(FetchUrl)
if (err != nil) { if err != nil {
log.Println(err) log.Println(err)
return return
} }
log.Println(file) log.Println(file)
} else if (RunAsServer) { } else if RunAsServer {
log.Printf("%s", ServerIP) log.Printf("%s", ServerIP)
runServer(ServerIP,ServerPort) runServer(ServerIP, ServerPort)
} else { } else {
if (len(RemoteHost) == 0) { if len(RemoteHost) == 0 {
log.Println("Please provide a remotehost!") log.Println("Please provide a remotehost!")
return 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!") log.Println("Please provide files to be uploaded!")
return return
} }
_,basename := filepath.Split(PutFile) _, basename := filepath.Split(PutFile)
queryParams := "?filename=" + basename queryParams := "?filename=" + basename
if (len(FileKeywords) > 0) { if len(FileKeywords) > 0 {
queryParams = queryParams + "&keywords=" + FileKeywords queryParams = queryParams + "&keywords=" + FileKeywords
} else { } else {
log.Println("WARN: you didn't provide any keywords :-(") log.Println("WARN: you didn't provide any keywords :-(")
} }
url, err := url.Parse(RemoteHost + "/f/" + queryParams) url, err := url.Parse(RemoteHost + "/f/" + queryParams)
if (err != nil) { if err != nil {
log.Println(err) log.Println(err)
return return
} }
log.Printf("POSTing: %s\n", url.String()) log.Printf("POSTing: %s\n", url.String())
url_path, err := PutFileFromPath(url.String(), PutFile) url_path, err := PutFileFromPath(url.String(), PutFile)
if (err != nil) { if err != nil {
log.Println(err) log.Println(err)
return return
} }
@ -162,7 +162,7 @@ func init() {
func loadConfiguration(filename string) (c Config) { func loadConfiguration(filename string) (c Config) {
//log.Printf("Attempting to load config file: %s", filename) //log.Printf("Attempting to load config file: %s", filename)
c, err := ReadConfigFile(filename) c, err := ReadConfigFile(filename)
if (err != nil) { if err != nil {
//log.Println(err) //log.Println(err)
return Config{} return Config{}
} }
@ -178,31 +178,30 @@ func loadConfiguration(filename string) (c Config) {
// Only set variables from config file, // Only set variables from config file,
// if they weren't passed as flags // if they weren't passed as flags
if (DefaultRunAsServer == RunAsServer && cRunAsServer) { if DefaultRunAsServer == RunAsServer && cRunAsServer {
RunAsServer = cRunAsServer RunAsServer = cRunAsServer
} }
if (DefaultServerIP == ServerIP && len(cServerIp) > 0) { if DefaultServerIP == ServerIP && len(cServerIp) > 0 {
ServerIP = cServerIp ServerIP = cServerIp
} }
if (DefaultServerPort == ServerPort && len(cServerPort) > 0) { if DefaultServerPort == ServerPort && len(cServerPort) > 0 {
ServerPort = cServerPort ServerPort = cServerPort
} }
if (DefaultMongoHost == MongoHost && len(cMongoHost) > 0) { if DefaultMongoHost == MongoHost && len(cMongoHost) > 0 {
MongoHost = cMongoHost MongoHost = cMongoHost
} }
if (DefaultMongoDB == MongoDB && len(cMongoDB) > 0) { if DefaultMongoDB == MongoDB && len(cMongoDB) > 0 {
MongoDB = cMongoDB MongoDB = cMongoDB
} }
if (DefaultMongoUsername == MongoUsername && len(cMongoUsername) > 0) { if DefaultMongoUsername == MongoUsername && len(cMongoUsername) > 0 {
MongoUsername = cMongoUsername MongoUsername = cMongoUsername
} }
if (DefaultMongoPassword == MongoPassword && len(cMongoPassword) > 0) { if DefaultMongoPassword == MongoPassword && len(cMongoPassword) > 0 {
MongoPassword = cMongoPassword MongoPassword = cMongoPassword
} }
if (DefaultRemoteHost == RemoteHost && len(cRemoteHost) > 0) { if DefaultRemoteHost == RemoteHost && len(cRemoteHost) > 0 {
RemoteHost = cRemoteHost RemoteHost = cRemoteHost
} }
return c return c
} }

View file

@ -8,7 +8,6 @@ import (
var emptyInterface interface{} var emptyInterface interface{}
var headTemplate = template.Must(template.New("head").Parse(headTemplateHTML)) var headTemplate = template.Must(template.New("head").Parse(headTemplateHTML))
var headTemplateHTML = ` var headTemplateHTML = `
<html> <html>
@ -128,86 +127,76 @@ var listTemplateHTML = `
` `
func UrliePage(w io.Writer) (err error) { func UrliePage(w io.Writer) (err error) {
err = headTemplate.Execute(w, map[string]string{"title" : "FileSrv :: URLie"}) err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: URLie"})
if (err != nil) { if err != nil {
return err return err
} }
err = navbarTemplate.Execute(w, nil) err = navbarTemplate.Execute(w, nil)
if (err != nil) { if err != nil {
return err return err
} }
err = containerBeginTemplate.Execute(w, nil) err = containerBeginTemplate.Execute(w, nil)
if (err != nil) { if err != nil {
return err return err
} }
err = formGetUrlTemplate.Execute(w, &emptyInterface) err = formGetUrlTemplate.Execute(w, &emptyInterface)
if (err != nil) { if err != nil {
return err return err
} }
err = tailTemplate.Execute(w, map[string]string{"footer" : fmt.Sprintf("Version: %s", VERSION)}) err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
if (err != nil) { if err != nil {
return err return err
} }
return return
} }
func UploadPage(w io.Writer) (err error) { func UploadPage(w io.Writer) (err error) {
err = headTemplate.Execute(w, map[string]string{"title" : "FileSrv :: Upload"}) err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: Upload"})
if (err != nil) { if err != nil {
return err return err
} }
err = navbarTemplate.Execute(w, nil) err = navbarTemplate.Execute(w, nil)
if (err != nil) { if err != nil {
return err return err
} }
err = containerBeginTemplate.Execute(w, nil) err = containerBeginTemplate.Execute(w, nil)
if (err != nil) { if err != nil {
return err return err
} }
err = formFileUploadTemplate.Execute(w, &emptyInterface) err = formFileUploadTemplate.Execute(w, &emptyInterface)
if (err != nil) { if err != nil {
return err return err
} }
err = tailTemplate.Execute(w, map[string]string{"footer" : fmt.Sprintf("Version: %s", VERSION)}) err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
if (err != nil) { if err != nil {
return err return err
} }
return return
} }
func ListFilesPage(w io.Writer, files []File) (err error) { func ListFilesPage(w io.Writer, files []File) (err error) {
err = headTemplate.Execute(w, map[string]string{"title" : "FileSrv"}) err = headTemplate.Execute(w, map[string]string{"title": "FileSrv"})
if (err != nil) { if err != nil {
return err return err
} }
err = navbarTemplate.Execute(w, nil) err = navbarTemplate.Execute(w, nil)
if (err != nil) { if err != nil {
return err return err
} }
err = containerBeginTemplate.Execute(w, nil) err = containerBeginTemplate.Execute(w, nil)
if (err != nil) { if err != nil {
return err return err
} }
err = listTemplate.Execute(w, files) err = listTemplate.Execute(w, files)
if (err != nil) { if err != nil {
return err return err
} }
err = tailTemplate.Execute(w, map[string]string{"footer" : fmt.Sprintf("Version: %s", VERSION)}) err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
if (err != nil) { if err != nil {
return err return err
} }
return return
} }
var bootstrapCSS = ` var bootstrapCSS = `
/*! /*!
* Bootstrap v2.2.2 * Bootstrap v2.2.2
@ -6249,5 +6238,3 @@ a.badge:hover {
position: fixed; position: fixed;
} }
` `

1185
server.go

File diff suppressed because it is too large Load diff