1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-11-14 20:58:37 +00:00

rearranging

This commit is contained in:
Vincent Batts 2013-02-11 11:00:57 -05:00
parent c9f9cf68bb
commit 3a899e1daa
3 changed files with 78 additions and 77 deletions

25
get.go
View file

@ -5,28 +5,3 @@ import (
"labix.org/v2/mgo/bson"
)
/* 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) {
return this_file, err
}
return this_file, nil
}
func GetFileRandom() (this_file File, err error) {
r := rand.Int63()
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 (err != nil) {
return this_file, err
}
return this_file, nil
}

26
has.go
View file

@ -2,9 +2,35 @@
package main
import (
"math/rand"
"labix.org/v2/mgo/bson"
)
/* 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) {
return this_file, err
}
return this_file, nil
}
func GetFileRandom() (this_file File, err error) {
r := rand.Int63()
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 (err != nil) {
return this_file, err
}
return this_file, nil
}
/* 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()

104
imgsrv.go
View file

@ -64,6 +64,58 @@ type File struct {
ContentType string "contentType,omitempty"
}
func main() {
flag.Parse()
for _, arg := range flag.Args() {
// What to do with these floating args ...
log.Printf("%s", arg)
}
// loads either default or flag specified config
// to override variables
loadConfiguration(ConfigFile)
if (len(FetchUrl) > 0) {
file, err := FetchFileFromURL(FetchUrl)
if (err != nil) {
log.Println(err)
return
}
log.Println(file)
} else if (RunAsServer) {
log.Printf("%s", ServerIP)
runServer(ServerIP,ServerPort)
} else {
if (len(RemoteHost) == 0) {
log.Println("Please provide a remotehost!")
return
}
if (len(PutFile) == 0 ) { //&& len(flag.Args()) == 0) {
log.Println("Please provide files to be uploaded!")
return
}
_,basename := filepath.Split(PutFile)
queryParams := "?filename=" + basename
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) {
log.Println(err)
return
}
log.Printf("POSTing: %s\n", url.String())
url_path, err := PutFileFromPath(url.String(), PutFile)
if (err != nil) {
log.Println(err)
return
}
log.Printf("New Image!: %s%s\n", RemoteHost, url_path)
}
}
/* http://golang.org/doc/effective_go.html#init */
func init() {
flag.StringVar(&ConfigFile,
@ -171,55 +223,3 @@ func loadConfiguration(filename string) (c Config) {
return c
}
func main() {
flag.Parse()
for _, arg := range flag.Args() {
// What to do with these floating args ...
log.Printf("%s", arg)
}
// loads either default or flag specified config
// to override variables
loadConfiguration(ConfigFile)
if (len(FetchUrl) > 0) {
file, err := FetchFileFromURL(FetchUrl)
if (err != nil) {
log.Println(err)
return
}
log.Println(file)
} else if (RunAsServer) {
log.Printf("%s", ServerIP)
runServer(ServerIP,ServerPort)
} else {
if (len(RemoteHost) == 0) {
log.Println("Please provide a remotehost!")
return
}
if (len(PutFile) == 0 ) { //&& len(flag.Args()) == 0) {
log.Println("Please provide files to be uploaded!")
return
}
_,basename := filepath.Split(PutFile)
queryParams := "?filename=" + basename
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) {
log.Println(err)
return
}
log.Printf("POSTing: %s\n", url.String())
url_path, err := PutFileFromPath(url.String(), PutFile)
if (err != nil) {
log.Println(err)
return
}
log.Printf("New Image!: %s%s\n", RemoteHost, url_path)
}
}