Skip to content
Snippets Groups Projects
Commit eb8e2553 authored by ale's avatar ale
Browse files

pass a FileStorage to Sync() so that it can find the files

parent 9285ee23
No related branches found
No related tags found
No related merge requests found
...@@ -150,8 +150,9 @@ func doUpdate(db *liber.Database, dir string) { ...@@ -150,8 +150,9 @@ func doUpdate(db *liber.Database, dir string) {
} }
func doSync(db *liber.Database, remoteAddr string) { func doSync(db *liber.Database, remoteAddr string) {
storage := liber.NewFileStorage(expandTilde(*bookDir))
sc := liber.NewRemoteServer(remoteAddr) sc := liber.NewRemoteServer(remoteAddr)
if err := db.Sync(sc); err != nil { if err := db.Sync(storage, sc); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
...@@ -178,8 +179,8 @@ func doSearch(db *liber.Database, query string) { ...@@ -178,8 +179,8 @@ func doSearch(db *liber.Database, query string) {
} }
func doHttpServer(db *liber.Database, addr string) { func doHttpServer(db *liber.Database, addr string) {
storage := liber.NewFileStorage(expandTilde(*bookDir), 2) storage := liber.NewRWFileStorage(expandTilde(*bookDir), 2)
cache := liber.NewFileStorage(filepath.Join(expandTilde(*databaseDir), "cache"), 2) cache := liber.NewRWFileStorage(filepath.Join(expandTilde(*databaseDir), "cache"), 2)
server := liber.NewHttpServer(db, storage, cache, addr) server := liber.NewHttpServer(db, storage, cache, addr)
log.Fatal(server.ListenAndServe()) log.Fatal(server.ListenAndServe())
} }
......
...@@ -23,7 +23,7 @@ const ( ...@@ -23,7 +23,7 @@ const (
type SyncClient interface { type SyncClient interface {
DiffRequest(*diffRequest) (*diffResponse, error) DiffRequest(*diffRequest) (*diffResponse, error)
SendBook(*Book, []*File) error SendBook(*Book, *FileStorage, []*File) error
} }
type remoteServer struct { type remoteServer struct {
...@@ -110,7 +110,7 @@ func addFilePart(w *multipart.Writer, varname, filename, mimeFilename string) er ...@@ -110,7 +110,7 @@ func addFilePart(w *multipart.Writer, varname, filename, mimeFilename string) er
} }
// SendBook uploads a book to the remote server. // SendBook uploads a book to the remote server.
func (r *remoteServer) SendBook(book *Book, files []*File) error { func (r *remoteServer) SendBook(book *Book, storage *FileStorage, files []*File) error {
// Create a multipart request with the JSON-encoded metadata // Create a multipart request with the JSON-encoded metadata
// and the actual file contents as two separate mime/multipart // and the actual file contents as two separate mime/multipart
// sections. // sections.
...@@ -125,14 +125,14 @@ func (r *remoteServer) SendBook(book *Book, files []*File) error { ...@@ -125,14 +125,14 @@ func (r *remoteServer) SendBook(book *Book, files []*File) error {
for i, f := range files { for i, f := range files {
varname := fmt.Sprintf("book%d", i) varname := fmt.Sprintf("book%d", i)
filename := fmt.Sprintf("%d%s", book.Id, f.FileType) filename := fmt.Sprintf("%d%s", book.Id, f.FileType)
if err := addFilePart(w, varname, f.Path, filename); err != nil { if err := addFilePart(w, varname, storage.Abs(f.Path), filename); err != nil {
w.Close() w.Close()
return err return err
} }
} }
if book.CoverPath != "" { if book.CoverPath != "" {
if err := addFilePart(w, "cover", book.CoverPath, "cover.jpg"); err != nil { if err := addFilePart(w, "cover", storage.Abs(book.CoverPath), "cover.jpg"); err != nil {
w.Close() w.Close()
return err return err
} }
...@@ -220,7 +220,7 @@ func (db *Database) findMissing(srv SyncClient) chan string { ...@@ -220,7 +220,7 @@ func (db *Database) findMissing(srv SyncClient) chan string {
// Sync the local database with a remote one. This is a one-way // Sync the local database with a remote one. This is a one-way
// synchronization: files missing on the remote side will be uploaded // synchronization: files missing on the remote side will be uploaded
// to it. // to it.
func (db *Database) Sync(remote SyncClient) error { func (db *Database) Sync(storage *FileStorage, remote SyncClient) error {
var wg sync.WaitGroup var wg sync.WaitGroup
ch := db.findMissing(remote) ch := db.findMissing(remote)
...@@ -232,7 +232,7 @@ func (db *Database) Sync(remote SyncClient) error { ...@@ -232,7 +232,7 @@ func (db *Database) Sync(remote SyncClient) error {
bookid := ParseID(id) bookid := ParseID(id)
if book, err := db.GetBook(bookid); err == nil { if book, err := db.GetBook(bookid); err == nil {
if files, err := db.GetBookFiles(bookid); err == nil { if files, err := db.GetBookFiles(bookid); err == nil {
if err := remote.SendBook(book, files); err != nil { if err := remote.SendBook(book, storage, files); err != nil {
log.Printf("SendBook(%s): %v", id, err) log.Printf("SendBook(%s): %v", id, err)
} }
} }
......
...@@ -24,6 +24,7 @@ func TestSync_Sync(t *testing.T) { ...@@ -24,6 +24,7 @@ func TestSync_Sync(t *testing.T) {
// Create a temporary directory to store uploads. // Create a temporary directory to store uploads.
updir, _ := ioutil.TempDir("", "ebook-upload-") updir, _ := ioutil.TempDir("", "ebook-upload-")
defer os.RemoveAll(updir) defer os.RemoveAll(updir)
clientStorage := NewFileStorage(updir)
td, db := newTestDatabase(t) td, db := newTestDatabase(t)
defer td.Close() defer td.Close()
...@@ -49,7 +50,7 @@ func TestSync_Sync(t *testing.T) { ...@@ -49,7 +50,7 @@ func TestSync_Sync(t *testing.T) {
defer srv.Close() defer srv.Close()
cl := NewRemoteServer(srv.URL) cl := NewRemoteServer(srv.URL)
err := db.Sync(cl) err := db.Sync(clientStorage, cl)
if err != nil { if err != nil {
t.Fatalf("Sync(): %v", err) t.Fatalf("Sync(): %v", err)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment