cleaning up manager, tying it to add files

This commit is contained in:
2020-06-19 17:12:21 -04:00
parent 7610e3f168
commit e75991da22
11 changed files with 140 additions and 148 deletions

View File

@@ -2,6 +2,7 @@ package manager
import (
"encoding/base64"
"fmt"
"path/filepath"
"strconv"
"strings"
@@ -20,7 +21,15 @@ func NewManager(rootDir string, version string, dbPath string, informer chan Ope
if err != nil {
return &Manager{}, err
}
patcher, err := engine.NewPatcher(log, dirPaths)
// Create new patcher
patcher := engine.Patcher{
log,
dirPaths.KeyFolder,
dirPaths.DownloadFolder,
dirPaths.SyncFolder,
dirPaths.ThumbFolder,
dirPaths.DiffFolder,
}
if err != nil {
log.Fatal().Msgf("Error creating a patcher %s", err)
return &Manager{}, err
@@ -86,12 +95,12 @@ func checkPaths(rootDir string) (filePaths *FilePaths, err error) {
// do checks to make sure that it is successfully monitoring it, and that there
// is a historical breadcrumb trail to recreate all the versions that the database
// claims to have a copy of
func (m *Manager) AddFileToRepo(file string, hardCopy bool) (string, error) {
func (m *Manager) AddFileToRepo(relFilePath string) error {
var err error
// the filepath should be absolute, but this should be done dynamically
if file, err = filepath.Abs(file); err != nil {
return "", err
}
// if file, err = filepath.Abs(file); err != nil {
// return "", err
// }
//TODO: what needs to happen is a channel for errors/progress is created
//then pass that channel to a routine, and put all of the following in it
// whenever an error returns, fire the string to the channel,
@@ -101,60 +110,45 @@ func (m *Manager) AddFileToRepo(file string, hardCopy bool) (string, error) {
//see commsManagment.go
// f := NewFileManager()
//DELAYED: this feature affects only large files and user experience. It can wait.
var tmpFile database.File
var filename string //we might aswell only verify the files validity once
filename := filepath.Base(relFilePath)
var hash [16]byte
//check that the file actually exists
if filename, err = engine.VerifySrcFile(file); err != nil {
//there was no source file or it was not recognisable as a file
return "", err
}
//check that the file actually exists (currently done by client/server)
// if filename, err = engine.VerifySrcFile(relFilePath); err != nil {
// //there was no source file or it was not recognisable as a file
// return "", err
// }
//generate a unique file name from the hash and the moment it was created
//a sampled (and therefore) fast, hash of the file for 'uniqueness'
if hash, err = engine.UniqueFileHash(file); err != nil {
return "", err
if hash, err = engine.UniqueFileHash(relFilePath); err != nil {
return err
}
if tmpFile, err = m.dB.CheckIfFileCurrentlyMonitored(file, hash); err != nil {
if tmpFile, err = m.dB.CheckIfFileCurrentlyMonitored(relFilePath, hash); err != nil {
if strings.Index(err.Error(), "not found") != -1 {
//the file wasn't found, this is an ok error
m.Info().Msgf("The file was [%s], so continuing to create it in the database", err)
} else {
m.Error().Msgf("Error checking if file [%s] is monitored so will init file. Error: %s", tmpFile.Path, err)
return fmt.Errorf("File was not found in repo, please add file first")
}
tmpFile.CurrentHash = hash
tmpFile.Name = filename
tmpFile.Path = file
tmpFile.Path = relFilePath
tmpFile.CreatedAt = time.Now()
tmpFile.Unique = base64.URLEncoding.EncodeToString([]byte(filename)) + "_" + base64.URLEncoding.EncodeToString((tmpFile.CurrentHash[:])) + "_" + strconv.FormatInt(tmpFile.CreatedAt.Unix(), 10) + "_" + filename
tmpFile.BkpLocation = filepath.Join(SyncFolder, tmpFile.Unique)
tmpFile.CurrentBase = tmpFile.BkpLocation
//tmpFile.BkpLocation = filepath.Join(m.SyncFolder, tmpFile.Unique)
//tmpFile.CurrentBase = tmpFile.BkpLocation
//tmpFile.Ignore = false //we can have files in the database that are ignored. TODO: This was initially added so that 'All Files' would show up as a file (its a hack as it adds a dummy to the database)
//we should now have a unique name for this file
//if needs be, we can find out the real file name from the string
//the hash will give us a reasononable indication of the similarity of the files
//the hash will give us a reasonable indication of the similarity of the files
//define filename of backup(s)
if _, err := m.prepareDatabaseForFile(tmpFile); err != nil {
return "", err
} else {
if err := m.copyFileToNewLocation(tmpFile.Path, tmpFile.BkpLocation, hardCopy); err != nil {
m.ErrorF("There was an error copying the file to the backup location %s", err)
return "", err
}
m.Informer <- Op_NewFile.Retrieve()
}
} else {
m.Debug().Msgf("file [%s] is already in the database. Assuming sync file in place", tmpFile.Path)
// we should check if the backup file exists, otherwise there is an issue
if _, err := engine.VerifySrcFile(tmpFile.BkpLocation); err != nil {
//if the backup doesn't exist, something has gone quite wrong....
m.Debug().Msgf("The backup file doesn't seem to exist at the expected location, %s", err)
return "", err
_, err := m.prepareDatabaseForFile(tmpFile)
if err != nil {
return err
}
}
return tmpFile.Path, m.watcher.Add(tmpFile.Path)
m.Info().Msgf("added file: %s at path: %s with hash: %s at time: %s", filename, relFilePath, tmpFile.CurrentHash, tmpFile.CreatedAt.String)
return nil
}
// prepareDatabaseForFile is responsible for keeping all references to the version of the file,
@@ -162,8 +156,8 @@ func (m *Manager) AddFileToRepo(file string, hardCopy bool) (string, error) {
//
// TODO: This will need to initialise a diff object in the database, currently created by the diff package,
// however going forward a diff maybe defined by the manager.
func (m *Manager) prepareDatabaseForFile(tmpFile engine.File) (int, error) {
fileID, err := m.dB.InitialiseFileInDatabase(tmpFile)
func (m *Manager) prepareDatabaseForFile(tmpFile database.File) (int, error) {
fileID, err := m.dB.InitializeFileInDatabase(tmpFile)
if err != nil {
m.Error().Msgf("Error checking if file [%s] is monitored. Error %s", tmpFile.Path, err)
return 0, err