2013-02-10 23:39:23 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-02-20 23:43:01 -05:00
|
|
|
"asink"
|
|
|
|
"asink/util"
|
|
|
|
"bytes"
|
2013-02-11 23:17:12 -05:00
|
|
|
"code.google.com/p/goconf/conf"
|
2013-02-18 20:44:00 -05:00
|
|
|
"database/sql"
|
2013-02-20 23:43:01 -05:00
|
|
|
"encoding/json"
|
2013-02-10 23:39:23 -05:00
|
|
|
"flag"
|
2013-02-11 23:17:12 -05:00
|
|
|
"fmt"
|
2013-02-20 23:43:01 -05:00
|
|
|
"net/http"
|
|
|
|
"io/ioutil"
|
2013-02-18 20:44:00 -05:00
|
|
|
"os"
|
2013-02-10 23:39:23 -05:00
|
|
|
"os/user"
|
2013-02-11 23:17:12 -05:00
|
|
|
"path"
|
2013-02-20 23:43:01 -05:00
|
|
|
"strconv"
|
2013-02-10 23:39:23 -05:00
|
|
|
)
|
|
|
|
|
2013-02-18 20:44:00 -05:00
|
|
|
type AsinkGlobals struct {
|
|
|
|
configFileName string
|
|
|
|
syncDir string
|
|
|
|
cacheDir string
|
|
|
|
tmpDir string
|
|
|
|
db *sql.DB
|
|
|
|
storage Storage
|
2013-02-20 23:43:01 -05:00
|
|
|
server string
|
|
|
|
port int
|
2013-02-18 20:44:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var globals AsinkGlobals
|
2013-02-10 23:39:23 -05:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
const config_usage = "Config File to use"
|
|
|
|
userHomeDir := "~"
|
|
|
|
|
|
|
|
u, err := user.Current()
|
|
|
|
if err == nil {
|
|
|
|
userHomeDir = u.HomeDir
|
2013-02-11 23:17:12 -05:00
|
|
|
}
|
2013-02-10 23:39:23 -05:00
|
|
|
|
2013-02-18 20:44:00 -05:00
|
|
|
flag.StringVar(&globals.configFileName, "config", path.Join(userHomeDir, ".asink", "config"), config_usage)
|
|
|
|
flag.StringVar(&globals.configFileName, "c", path.Join(userHomeDir, ".asink", "config"), config_usage+" (shorthand)")
|
2013-02-10 23:39:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2013-02-18 20:44:00 -05:00
|
|
|
fmt.Println("config file:", globals.configFileName)
|
2013-02-10 23:39:23 -05:00
|
|
|
|
2013-02-18 20:44:00 -05:00
|
|
|
config, err := conf.ReadConfigFile(globals.configFileName)
|
2013-02-10 23:39:23 -05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2013-02-18 20:44:00 -05:00
|
|
|
fmt.Println("Error reading config file at ", globals.configFileName, ". Does it exist?")
|
2013-02-10 23:39:23 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:44:00 -05:00
|
|
|
globals.storage, err = GetStorage(config)
|
2013-02-10 23:39:23 -05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:44:00 -05:00
|
|
|
globals.syncDir, err = config.GetString("local", "syncdir")
|
|
|
|
globals.cacheDir, err = config.GetString("local", "cachedir")
|
|
|
|
globals.tmpDir, err = config.GetString("local", "tmpdir")
|
|
|
|
|
|
|
|
//make sure all the necessary directories exist
|
2013-02-20 23:43:01 -05:00
|
|
|
err = util.EnsureDirExists(globals.syncDir)
|
2013-02-18 20:44:00 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-20 23:43:01 -05:00
|
|
|
err = util.EnsureDirExists(globals.cacheDir)
|
2013-02-18 20:44:00 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-20 23:43:01 -05:00
|
|
|
err = util.EnsureDirExists(globals.tmpDir)
|
2013-02-18 20:44:00 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-10 23:39:23 -05:00
|
|
|
|
2013-02-18 20:44:00 -05:00
|
|
|
//TODO FIXME REMOVEME
|
|
|
|
fmt.Println(globals.syncDir)
|
|
|
|
fmt.Println(globals.cacheDir)
|
|
|
|
fmt.Println(globals.tmpDir)
|
|
|
|
fmt.Println(globals.storage)
|
|
|
|
//TODO FIXME REMOVEME
|
2013-02-10 23:39:23 -05:00
|
|
|
|
2013-02-20 23:43:01 -05:00
|
|
|
globals.server, err = config.GetString("server", "host")
|
|
|
|
globals.port, err = config.GetInt("server", "port")
|
|
|
|
|
|
|
|
fileUpdates := make(chan *asink.Event)
|
2013-02-18 20:44:00 -05:00
|
|
|
go StartWatching(globals.syncDir, fileUpdates)
|
|
|
|
|
|
|
|
globals.db, err = GetAndInitDB(config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
return
|
|
|
|
}
|
2013-02-10 23:39:23 -05:00
|
|
|
|
2013-02-11 23:16:19 -05:00
|
|
|
for {
|
2013-02-11 23:17:12 -05:00
|
|
|
event := <-fileUpdates
|
2013-02-18 20:44:00 -05:00
|
|
|
ProcessEvent(globals, event)
|
2013-02-10 23:39:23 -05:00
|
|
|
}
|
2013-02-11 23:16:19 -05:00
|
|
|
}
|
2013-02-10 23:39:23 -05:00
|
|
|
|
2013-02-20 23:43:01 -05:00
|
|
|
func ProcessEvent(globals AsinkGlobals, event *asink.Event) {
|
2013-02-18 20:44:00 -05:00
|
|
|
//add to database
|
|
|
|
err := DatabaseAddEvent(globals.db, event)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-10 23:39:23 -05:00
|
|
|
|
2013-02-11 23:16:19 -05:00
|
|
|
if event.IsUpdate() {
|
2013-02-18 20:44:00 -05:00
|
|
|
//copy to tmp
|
2013-02-20 23:43:01 -05:00
|
|
|
tmpfilename, err := util.CopyToTmp(event.Path, globals.tmpDir)
|
2013-02-18 20:44:00 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-20 23:43:01 -05:00
|
|
|
event.Status |= asink.COPIED_TO_TMP
|
2013-02-18 20:44:00 -05:00
|
|
|
|
|
|
|
//get the file's hash
|
|
|
|
hash, err := HashFile(tmpfilename)
|
|
|
|
event.Hash = hash
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-20 23:43:01 -05:00
|
|
|
event.Status |= asink.HASHED
|
2013-02-18 20:44:00 -05:00
|
|
|
|
|
|
|
//rename to local cache w/ filename=hash
|
|
|
|
err = os.Rename(tmpfilename, path.Join(globals.cacheDir, event.Hash))
|
|
|
|
if err != nil {
|
|
|
|
err := os.Remove(tmpfilename)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2013-02-20 23:43:01 -05:00
|
|
|
event.Status |= asink.CACHED
|
2013-02-18 20:44:00 -05:00
|
|
|
|
|
|
|
//update database
|
|
|
|
err = DatabaseUpdateEvent(globals.db, event)
|
2013-02-11 23:17:12 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-18 20:44:00 -05:00
|
|
|
|
|
|
|
//upload file to remote storage
|
|
|
|
err = globals.storage.Put(event.Path, event.Hash)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-20 23:43:01 -05:00
|
|
|
event.Status |= asink.UPLOADED
|
2013-02-18 20:44:00 -05:00
|
|
|
|
|
|
|
//update database again
|
|
|
|
err = DatabaseUpdateEvent(globals.db, event)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2013-02-10 23:39:23 -05:00
|
|
|
}
|
2013-02-18 20:44:00 -05:00
|
|
|
|
2013-02-20 23:43:01 -05:00
|
|
|
//finally, send it off to the server
|
|
|
|
url := "http://" + globals.server + ":" + strconv.Itoa(int(globals.port)) + "/events/"
|
|
|
|
|
|
|
|
//construct json payload
|
|
|
|
events := asink.EventList{
|
|
|
|
Events: []*asink.Event{event},
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(events)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println(string(b))
|
|
|
|
|
|
|
|
//actually make the request
|
|
|
|
resp, err := http.Post(url, "application/json", bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
//check to make sure request succeeded
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var apistatus asink.APIResponse
|
|
|
|
err = json.Unmarshal(body, &apistatus)
|
|
|
|
if err != nil {
|
|
|
|
panic(err) //TODO handle sensibly
|
|
|
|
}
|
|
|
|
if apistatus.Status != "success" {
|
|
|
|
panic("Status not success") //TODO handle sensibly
|
|
|
|
}
|
|
|
|
fmt.Println(apistatus)
|
|
|
|
|
|
|
|
event.Status |= asink.ON_SERVER
|
|
|
|
err = DatabaseUpdateEvent(globals.db, event)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-10 23:39:23 -05:00
|
|
|
}
|