mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-14 12:48:37 +00:00
running go fmt
This commit is contained in:
parent
7ba7c07041
commit
9a5ded48a6
10 changed files with 906 additions and 925 deletions
119
client.go
119
client.go
|
@ -1,75 +1,74 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
func PutFileFromPath(host, filename string) (path string, err error) {
|
||||
ext := filepath.Ext(filename)
|
||||
file, err := os.Open(filename)
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
resp, err := http.Post(host, mime.TypeByExtension(ext) , bufio.NewReader(file))
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
bytes, err := ioutil.ReadAll(resp.Body)
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
return string(bytes), nil
|
||||
ext := filepath.Ext(filename)
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
resp, err := http.Post(host, mime.TypeByExtension(ext), bufio.NewReader(file))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
bytes, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
|
||||
func FetchFileFromURL(url string) (filename string, err error) {
|
||||
var t time.Time
|
||||
var t time.Time
|
||||
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{ InsecureSkipVerify: true },
|
||||
}
|
||||
client := &http.Client{
|
||||
//CheckRedirect: redirectPolicyFunc,
|
||||
Transport: tr,
|
||||
}
|
||||
resp, err := client.Get(url)
|
||||
defer resp.Body.Close()
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
client := &http.Client{
|
||||
//CheckRedirect: redirectPolicyFunc,
|
||||
Transport: tr,
|
||||
}
|
||||
resp, err := client.Get(url)
|
||||
defer resp.Body.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
mtime := resp.Header.Get("last-modified")
|
||||
if (len(mtime) > 0) {
|
||||
t, err = time.Parse(http.TimeFormat, mtime)
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
log.Println("Last-Modified not present. Using current time")
|
||||
t = time.Now()
|
||||
}
|
||||
_, url_filename := filepath.Split(url)
|
||||
mtime := resp.Header.Get("last-modified")
|
||||
if len(mtime) > 0 {
|
||||
t, err = time.Parse(http.TimeFormat, mtime)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
log.Println("Last-Modified not present. Using current time")
|
||||
t = time.Now()
|
||||
}
|
||||
_, url_filename := filepath.Split(url)
|
||||
|
||||
log.Println(resp)
|
||||
log.Println(resp)
|
||||
|
||||
bytes, err := ioutil.ReadAll(resp.Body)
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(os.TempDir(), url_filename), bytes, 0644)
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
err = os.Chtimes(filepath.Join(os.TempDir(), url_filename), t, t)
|
||||
bytes, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(os.TempDir(), url_filename), bytes, 0644)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = os.Chtimes(filepath.Join(os.TempDir(), url_filename), t, t)
|
||||
|
||||
// lastly, return
|
||||
return filepath.Join(os.TempDir(), url_filename), nil
|
||||
// lastly, return
|
||||
return filepath.Join(os.TempDir(), url_filename), nil
|
||||
}
|
||||
|
||||
|
|
49
config.go
49
config.go
|
@ -1,44 +1,45 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"launchpad.net/goyaml"
|
||||
"io/ioutil"
|
||||
"io/ioutil"
|
||||
"launchpad.net/goyaml"
|
||||
)
|
||||
|
||||
type Config map[string]interface{}
|
||||
|
||||
func (c *Config) GetBool(option string) (value bool) {
|
||||
conf := Config{}
|
||||
conf = *c
|
||||
switch conf[option] {
|
||||
default: value = false
|
||||
case "yes", "on", "true": value = true
|
||||
}
|
||||
return
|
||||
conf := Config{}
|
||||
conf = *c
|
||||
switch conf[option] {
|
||||
default:
|
||||
value = false
|
||||
case "yes", "on", "true":
|
||||
value = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Config) GetString(option string) (value string) {
|
||||
conf := Config{}
|
||||
conf = *c
|
||||
value, _ = conf[option].(string)
|
||||
return
|
||||
conf := Config{}
|
||||
conf = *c
|
||||
value, _ = conf[option].(string)
|
||||
return
|
||||
}
|
||||
|
||||
func ReadConfigFile(filename string) (config Config, err error) {
|
||||
bytes, err := ioutil.ReadFile(filename)
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
bytes, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = goyaml.Unmarshal(bytes, &config)
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
err = goyaml.Unmarshal(bytes, &config)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return config, nil
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func WriteConfigFile(filename string, data []byte) (err error) {
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -1,60 +1,58 @@
|
|||
|
||||
package main
|
||||
|
||||
import (
|
||||
"labix.org/v2/mgo/bson"
|
||||
"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
|
||||
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 := Rand64()
|
||||
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
|
||||
r := Rand64()
|
||||
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()
|
||||
if (err != nil) {
|
||||
return false, err
|
||||
}
|
||||
exists = (c > 0)
|
||||
return exists, nil
|
||||
c, err := gfs.Find(bson.M{"filename": filename}).Count()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
exists = (c > 0)
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func HasFileByMd5(md5 string) (exists bool, err error) {
|
||||
c, err := gfs.Find(bson.M{"md5": md5 }).Count()
|
||||
if (err != nil) {
|
||||
return false, err
|
||||
}
|
||||
exists = (c > 0)
|
||||
return exists, nil
|
||||
c, err := gfs.Find(bson.M{"md5": md5}).Count()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
exists = (c > 0)
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func HasFileByKeyword(keyword string) (exists bool, err error) {
|
||||
c, err := gfs.Find(bson.M{"metadata": bson.M{"keywords": keyword} }).Count()
|
||||
if (err != nil) {
|
||||
return false, err
|
||||
}
|
||||
exists = (c > 0)
|
||||
return exists, 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
|
||||
}
|
||||
|
||||
|
|
37
hash.go
37
hash.go
|
@ -1,38 +1,37 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"hash/adler32"
|
||||
"io"
|
||||
"math/rand"
|
||||
"time"
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"hash/adler32"
|
||||
"io"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Rand64() int64 {
|
||||
return rand.Int63()
|
||||
return rand.Int63()
|
||||
}
|
||||
|
||||
/* Convinience method for getting md5 sum of a string */
|
||||
func GetMd5FromString(blob string) (sum []byte) {
|
||||
h := md5.New()
|
||||
defer h.Reset()
|
||||
io.WriteString(h,blob)
|
||||
return h.Sum(nil)
|
||||
h := md5.New()
|
||||
defer h.Reset()
|
||||
io.WriteString(h, blob)
|
||||
return h.Sum(nil)
|
||||
}
|
||||
|
||||
/* Convinience method for getting md5 sum of some bytes */
|
||||
func GetMd5FromBytes(blob []byte) (sum []byte) {
|
||||
h := md5.New()
|
||||
defer h.Reset()
|
||||
h.Write(blob)
|
||||
return h.Sum(nil)
|
||||
h := md5.New()
|
||||
defer h.Reset()
|
||||
h.Write(blob)
|
||||
return h.Sum(nil)
|
||||
}
|
||||
|
||||
/* get a small, decently unique hash */
|
||||
func GetSmallHash() (small_hash string) {
|
||||
h := adler32.New()
|
||||
io.WriteString(h, fmt.Sprintf("%d", time.Now().Unix()))
|
||||
return fmt.Sprintf("%X", h.Sum(nil))
|
||||
h := adler32.New()
|
||||
io.WriteString(h, fmt.Sprintf("%d", time.Now().Unix()))
|
||||
return fmt.Sprintf("%X", h.Sum(nil))
|
||||
}
|
||||
|
||||
|
|
47
hash_test.go
47
hash_test.go
|
@ -1,40 +1,39 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRand64(t *testing.T) {
|
||||
var i interface{}
|
||||
i = Rand64()
|
||||
v, ok := i.(int64)
|
||||
if (!ok) {
|
||||
t.Errorf("Rand64 returned wrong type")
|
||||
}
|
||||
if (v < 0) {
|
||||
t.Errorf("Rand64 returned a too small number [%d]", v)
|
||||
}
|
||||
var i interface{}
|
||||
i = Rand64()
|
||||
v, ok := i.(int64)
|
||||
if !ok {
|
||||
t.Errorf("Rand64 returned wrong type")
|
||||
}
|
||||
if v < 0 {
|
||||
t.Errorf("Rand64 returned a too small number [%d]", v)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHash(t *testing.T) {
|
||||
}
|
||||
|
||||
|
|
339
imgsrv.go
339
imgsrv.go
|
@ -8,201 +8,200 @@ package main
|
|||
*/
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"labix.org/v2/mgo"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"flag"
|
||||
"fmt"
|
||||
"labix.org/v2/mgo"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var (
|
||||
ConfigFile = fmt.Sprintf("%s/.imgsrv.yaml", os.Getenv("HOME"))
|
||||
ConfigFile = fmt.Sprintf("%s/.imgsrv.yaml", os.Getenv("HOME"))
|
||||
|
||||
DefaultRunAsServer = false
|
||||
RunAsServer = DefaultRunAsServer
|
||||
DefaultRunAsServer = false
|
||||
RunAsServer = DefaultRunAsServer
|
||||
|
||||
DefaultServerIP = "0.0.0.0"
|
||||
ServerIP = DefaultServerIP
|
||||
DefaultServerPort = "7777"
|
||||
ServerPort = DefaultServerPort
|
||||
DefaultMongoHost = "localhost"
|
||||
MongoHost = DefaultMongoHost
|
||||
DefaultMongoDB = "filesrv"
|
||||
MongoDB = DefaultMongoDB
|
||||
DefaultMongoUsername = ""
|
||||
MongoUsername = DefaultMongoUsername
|
||||
DefaultMongoPassword = ""
|
||||
MongoPassword = DefaultMongoPassword
|
||||
DefaultServerIP = "0.0.0.0"
|
||||
ServerIP = DefaultServerIP
|
||||
DefaultServerPort = "7777"
|
||||
ServerPort = DefaultServerPort
|
||||
DefaultMongoHost = "localhost"
|
||||
MongoHost = DefaultMongoHost
|
||||
DefaultMongoDB = "filesrv"
|
||||
MongoDB = DefaultMongoDB
|
||||
DefaultMongoUsername = ""
|
||||
MongoUsername = DefaultMongoUsername
|
||||
DefaultMongoPassword = ""
|
||||
MongoPassword = DefaultMongoPassword
|
||||
|
||||
mongo_session *mgo.Session
|
||||
images_db *mgo.Database
|
||||
gfs *mgo.GridFS
|
||||
mongo_session *mgo.Session
|
||||
images_db *mgo.Database
|
||||
gfs *mgo.GridFS
|
||||
|
||||
DefaultRemoteHost = ""
|
||||
RemoteHost = DefaultRemoteHost
|
||||
PutFile = ""
|
||||
FetchUrl = ""
|
||||
FileKeywords = ""
|
||||
DefaultRemoteHost = ""
|
||||
RemoteHost = DefaultRemoteHost
|
||||
PutFile = ""
|
||||
FetchUrl = ""
|
||||
FileKeywords = ""
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
for _, arg := range flag.Args() {
|
||||
// What to do with these floating args ...
|
||||
log.Printf("%s", arg)
|
||||
}
|
||||
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)
|
||||
// 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)
|
||||
}
|
||||
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,
|
||||
"config",
|
||||
ConfigFile,
|
||||
"Provide alternate configuration file")
|
||||
flag.StringVar(&ConfigFile,
|
||||
"config",
|
||||
ConfigFile,
|
||||
"Provide alternate configuration file")
|
||||
|
||||
/* Server-side */
|
||||
flag.BoolVar(&RunAsServer,
|
||||
"server",
|
||||
RunAsServer,
|
||||
"Run as an image server (defaults to client-side)")
|
||||
flag.StringVar(&ServerIP,
|
||||
"ip",
|
||||
ServerIP,
|
||||
"IP to bind to (if running as a server)('ip' in the config)")
|
||||
flag.StringVar(&ServerPort,
|
||||
"port",
|
||||
ServerPort,
|
||||
"Port to listen on (if running as a server)('port' in the config)")
|
||||
/* Server-side */
|
||||
flag.BoolVar(&RunAsServer,
|
||||
"server",
|
||||
RunAsServer,
|
||||
"Run as an image server (defaults to client-side)")
|
||||
flag.StringVar(&ServerIP,
|
||||
"ip",
|
||||
ServerIP,
|
||||
"IP to bind to (if running as a server)('ip' in the config)")
|
||||
flag.StringVar(&ServerPort,
|
||||
"port",
|
||||
ServerPort,
|
||||
"Port to listen on (if running as a server)('port' in the config)")
|
||||
|
||||
/* MongoDB settings */
|
||||
flag.StringVar(&MongoHost,
|
||||
"mongo-host",
|
||||
MongoHost,
|
||||
"Mongo Host to connect to ('mongohost' in the config)")
|
||||
flag.StringVar(&MongoDB,
|
||||
"mongo-db",
|
||||
MongoDB,
|
||||
"Mongo db to connect to ('mongodb' in the config)")
|
||||
flag.StringVar(&MongoUsername,
|
||||
"mongo-username",
|
||||
MongoUsername,
|
||||
"Mongo username to auth with (if needed) ('mongousername' in the config)")
|
||||
flag.StringVar(&MongoPassword,
|
||||
"mongo-password",
|
||||
MongoPassword,
|
||||
"Mongo password to auth with (if needed) ('mongopassword' in the config)")
|
||||
/* MongoDB settings */
|
||||
flag.StringVar(&MongoHost,
|
||||
"mongo-host",
|
||||
MongoHost,
|
||||
"Mongo Host to connect to ('mongohost' in the config)")
|
||||
flag.StringVar(&MongoDB,
|
||||
"mongo-db",
|
||||
MongoDB,
|
||||
"Mongo db to connect to ('mongodb' in the config)")
|
||||
flag.StringVar(&MongoUsername,
|
||||
"mongo-username",
|
||||
MongoUsername,
|
||||
"Mongo username to auth with (if needed) ('mongousername' in the config)")
|
||||
flag.StringVar(&MongoPassword,
|
||||
"mongo-password",
|
||||
MongoPassword,
|
||||
"Mongo password to auth with (if needed) ('mongopassword' in the config)")
|
||||
|
||||
/* Client-side */
|
||||
flag.StringVar(&FetchUrl,
|
||||
"fetch",
|
||||
FetchUrl,
|
||||
"Just fetch the file from this url")
|
||||
/* Client-side */
|
||||
flag.StringVar(&FetchUrl,
|
||||
"fetch",
|
||||
FetchUrl,
|
||||
"Just fetch the file from this url")
|
||||
|
||||
flag.StringVar(&RemoteHost,
|
||||
"remotehost",
|
||||
RemoteHost,
|
||||
"Remote host to get/put files on ('remotehost' in the config)")
|
||||
flag.StringVar(&PutFile,
|
||||
"put",
|
||||
PutFile,
|
||||
"Put file on remote server (needs -remotehost)")
|
||||
flag.StringVar(&FileKeywords,
|
||||
"keywords",
|
||||
FileKeywords,
|
||||
"Keywords to associate with file. (comma delimited) (needs -put)")
|
||||
flag.StringVar(&RemoteHost,
|
||||
"remotehost",
|
||||
RemoteHost,
|
||||
"Remote host to get/put files on ('remotehost' in the config)")
|
||||
flag.StringVar(&PutFile,
|
||||
"put",
|
||||
PutFile,
|
||||
"Put file on remote server (needs -remotehost)")
|
||||
flag.StringVar(&FileKeywords,
|
||||
"keywords",
|
||||
FileKeywords,
|
||||
"Keywords to associate with file. (comma delimited) (needs -put)")
|
||||
|
||||
}
|
||||
|
||||
func loadConfiguration(filename string) (c Config) {
|
||||
//log.Printf("Attempting to load config file: %s", filename)
|
||||
c, err := ReadConfigFile(filename)
|
||||
if (err != nil) {
|
||||
//log.Println(err)
|
||||
return Config{}
|
||||
}
|
||||
//log.Printf("Attempting to load config file: %s", filename)
|
||||
c, err := ReadConfigFile(filename)
|
||||
if err != nil {
|
||||
//log.Println(err)
|
||||
return Config{}
|
||||
}
|
||||
|
||||
cRunAsServer := c.GetBool("server")
|
||||
cServerIp := c.GetString("ip")
|
||||
cServerPort := c.GetString("port")
|
||||
cMongoHost := c.GetString("mongohost")
|
||||
cMongoDB := c.GetString("mongodb")
|
||||
cMongoUsername := c.GetString("mongousername")
|
||||
cMongoPassword := c.GetString("mongopassword")
|
||||
cRemoteHost := c.GetString("remotehost")
|
||||
cRunAsServer := c.GetBool("server")
|
||||
cServerIp := c.GetString("ip")
|
||||
cServerPort := c.GetString("port")
|
||||
cMongoHost := c.GetString("mongohost")
|
||||
cMongoDB := c.GetString("mongodb")
|
||||
cMongoUsername := c.GetString("mongousername")
|
||||
cMongoPassword := c.GetString("mongopassword")
|
||||
cRemoteHost := c.GetString("remotehost")
|
||||
|
||||
// Only set variables from config file,
|
||||
// if they weren't passed as flags
|
||||
if (DefaultRunAsServer == RunAsServer && cRunAsServer) {
|
||||
RunAsServer = cRunAsServer
|
||||
}
|
||||
if (DefaultServerIP == ServerIP && len(cServerIp) > 0) {
|
||||
ServerIP = cServerIp
|
||||
}
|
||||
if (DefaultServerPort == ServerPort && len(cServerPort) > 0) {
|
||||
ServerPort = cServerPort
|
||||
}
|
||||
if (DefaultMongoHost == MongoHost && len(cMongoHost) > 0) {
|
||||
MongoHost = cMongoHost
|
||||
}
|
||||
if (DefaultMongoDB == MongoDB && len(cMongoDB) > 0) {
|
||||
MongoDB = cMongoDB
|
||||
}
|
||||
if (DefaultMongoUsername == MongoUsername && len(cMongoUsername) > 0) {
|
||||
MongoUsername = cMongoUsername
|
||||
}
|
||||
if (DefaultMongoPassword == MongoPassword && len(cMongoPassword) > 0) {
|
||||
MongoPassword = cMongoPassword
|
||||
}
|
||||
if (DefaultRemoteHost == RemoteHost && len(cRemoteHost) > 0) {
|
||||
RemoteHost = cRemoteHost
|
||||
}
|
||||
// Only set variables from config file,
|
||||
// if they weren't passed as flags
|
||||
if DefaultRunAsServer == RunAsServer && cRunAsServer {
|
||||
RunAsServer = cRunAsServer
|
||||
}
|
||||
if DefaultServerIP == ServerIP && len(cServerIp) > 0 {
|
||||
ServerIP = cServerIp
|
||||
}
|
||||
if DefaultServerPort == ServerPort && len(cServerPort) > 0 {
|
||||
ServerPort = cServerPort
|
||||
}
|
||||
if DefaultMongoHost == MongoHost && len(cMongoHost) > 0 {
|
||||
MongoHost = cMongoHost
|
||||
}
|
||||
if DefaultMongoDB == MongoDB && len(cMongoDB) > 0 {
|
||||
MongoDB = cMongoDB
|
||||
}
|
||||
if DefaultMongoUsername == MongoUsername && len(cMongoUsername) > 0 {
|
||||
MongoUsername = cMongoUsername
|
||||
}
|
||||
if DefaultMongoPassword == MongoPassword && len(cMongoPassword) > 0 {
|
||||
MongoPassword = cMongoPassword
|
||||
}
|
||||
if DefaultRemoteHost == RemoteHost && len(cRemoteHost) > 0 {
|
||||
RemoteHost = cRemoteHost
|
||||
}
|
||||
|
||||
return c
|
||||
return c
|
||||
}
|
||||
|
||||
|
|
145
layouts.go
145
layouts.go
|
@ -1,14 +1,13 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"text/template"
|
||||
"fmt"
|
||||
"io"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
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) {
|
||||
return err
|
||||
}
|
||||
err = navbarTemplate.Execute(w, nil)
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
err = containerBeginTemplate.Execute(w, nil)
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
err = formGetUrlTemplate.Execute(w, &emptyInterface)
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
err = tailTemplate.Execute(w, map[string]string{"footer" : fmt.Sprintf("Version: %s", VERSION)})
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
return
|
||||
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: URLie"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = navbarTemplate.Execute(w, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = containerBeginTemplate.Execute(w, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = formGetUrlTemplate.Execute(w, &emptyInterface)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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) {
|
||||
return err
|
||||
}
|
||||
err = navbarTemplate.Execute(w, nil)
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
err = containerBeginTemplate.Execute(w, nil)
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
err = formFileUploadTemplate.Execute(w, &emptyInterface)
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
err = tailTemplate.Execute(w, map[string]string{"footer" : fmt.Sprintf("Version: %s", VERSION)})
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
return
|
||||
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: Upload"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = navbarTemplate.Execute(w, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = containerBeginTemplate.Execute(w, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = formFileUploadTemplate.Execute(w, &emptyInterface)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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) {
|
||||
return err
|
||||
}
|
||||
err = navbarTemplate.Execute(w, nil)
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
err = containerBeginTemplate.Execute(w, nil)
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
err = listTemplate.Execute(w, files)
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
err = tailTemplate.Execute(w, map[string]string{"footer" : fmt.Sprintf("Version: %s", VERSION)})
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
return
|
||||
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = navbarTemplate.Execute(w, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = containerBeginTemplate.Execute(w, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = listTemplate.Execute(w, files)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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;
|
||||
}
|
||||
`
|
||||
|
||||
|
||||
|
|
22
types.go
22
types.go
|
@ -3,18 +3,18 @@ package main
|
|||
import "time"
|
||||
|
||||
type Info struct {
|
||||
Keywords []string // tags
|
||||
Ip string // who uploaded it
|
||||
Random int64
|
||||
TimeStamp time.Time "timestamp,omitempty"
|
||||
Keywords []string // tags
|
||||
Ip string // who uploaded it
|
||||
Random int64
|
||||
TimeStamp time.Time "timestamp,omitempty"
|
||||
}
|
||||
|
||||
type File struct {
|
||||
Metadata Info ",omitempty"
|
||||
Md5 string
|
||||
ChunkSize int
|
||||
UploadDate time.Time
|
||||
Length int64
|
||||
Filename string ",omitempty"
|
||||
ContentType string "contentType,omitempty"
|
||||
Metadata Info ",omitempty"
|
||||
Md5 string
|
||||
ChunkSize int
|
||||
UploadDate time.Time
|
||||
Length int64
|
||||
Filename string ",omitempty"
|
||||
ContentType string "contentType,omitempty"
|
||||
}
|
||||
|
|
10
version.go
10
version.go
|
@ -1,12 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
MAJOR int = 1
|
||||
MINOR int = 0
|
||||
TINY int = 0
|
||||
VERSION string = fmt.Sprintf("%d.%d.%d", MAJOR, MINOR, TINY)
|
||||
MAJOR int = 1
|
||||
MINOR int = 0
|
||||
TINY int = 0
|
||||
VERSION string = fmt.Sprintf("%d.%d.%d", MAJOR, MINOR, TINY)
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue