diff --git a/admin/main.go b/admin/main.go index 907c04c7..545cf827 100644 --- a/admin/main.go +++ b/admin/main.go @@ -128,6 +128,9 @@ func main() { sync.GlobalImporter, _ = sync.NewCloudImporter(cloudDAVBase, cloudUsername, cloudPassword) } if sync.GlobalImporter != nil { + if err := sync.GlobalImporter.Init(); err != nil { + log.Fatal("Unable to initialize the importer:", err) + } log.Println("Using", sync.GlobalImporter.Kind()) } diff --git a/admin/sync/exercices.go b/admin/sync/exercices.go index afe43038..bf42978f 100644 --- a/admin/sync/exercices.go +++ b/admin/sync/exercices.go @@ -243,6 +243,12 @@ func SyncExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic // SyncExercices imports new or updates existing exercices, in a given theme. func SyncExercices(i Importer, theme fic.Theme) (errs []string) { + if !avoidImporterSync() { + if err := i.Sync(); err != nil { + errs = append(errs, err.Error()) + } + } + if exercices, err := GetExercices(i, theme); err != nil { errs = append(errs, err.Error()) } else { diff --git a/admin/sync/file.go b/admin/sync/file.go index bc3749fe..b6d5d970 100644 --- a/admin/sync/file.go +++ b/admin/sync/file.go @@ -19,6 +19,10 @@ import ( type Importer interface { // Kind returns information about the Importer, for human interrest. Kind() string + // init performs the importer initialization. + Init() error + // sync tries to pull the latest modification of the underlying storage. + Sync() error // exists checks if the given location exists from the Importer point of view. exists(filename string) bool // toURL gets the full path/URL to the given file, the Importer will look internaly (used for debuging purpose). diff --git a/admin/sync/full.go b/admin/sync/full.go index f45d5f92..64aeccb0 100644 --- a/admin/sync/full.go +++ b/admin/sync/full.go @@ -22,6 +22,11 @@ var oneThemeDeepSync sync.Mutex // DeepSyncProgress expose the progression of the depp synchronization (0 = 0%, 255 = 100%). var DeepSyncProgress uint8 +// avoidImporterSync checks if Sync should be called or not. +func avoidImporterSync() bool { + return DeepSyncProgress > 1 && DeepSyncProgress < 255 +} + // SpeedySyncDeep performs a recursive synchronisation without importing files. func SpeedySyncDeep(i Importer) (errs map[string][]string) { oneDeepSync.Lock() @@ -36,6 +41,11 @@ func SpeedySyncDeep(i Importer) (errs map[string][]string) { errs = map[string][]string{} startTime := time.Now() + + if err := i.Sync(); err != nil { + errs["_sync"] = []string{err.Error()} + } + errs["_date"] = []string{fmt.Sprintf("%v", startTime)} errs["_themes"] = SyncThemes(i) @@ -89,6 +99,11 @@ func SyncDeep(i Importer) (errs map[string][]string) { errs = map[string][]string{} startTime := time.Now() + + if err := i.Sync(); err != nil { + errs["_sync"] = []string{err.Error()} + } + errs["_date"] = []string{fmt.Sprintf("%v", startTime)} errs["_themes"] = SyncThemes(i) @@ -171,6 +186,10 @@ func SyncThemeDeep(i Importer, theme fic.Theme, tid int, themeStep uint8) (errs oneThemeDeepSync.Lock() defer oneThemeDeepSync.Unlock() + if err := i.Sync(); err != nil { + errs = append(errs, err.Error()) + } + DeepSyncProgress = 3 + uint8(tid)*themeStep errs = SyncExercices(i, theme) diff --git a/admin/sync/importer_cloud.go b/admin/sync/importer_cloud.go index 70811e6d..5cbf1731 100644 --- a/admin/sync/importer_cloud.go +++ b/admin/sync/importer_cloud.go @@ -38,6 +38,14 @@ func (i CloudImporter) Kind() string { return "cloud file importer: " + i.baseDAV.String() } +func (i CloudImporter) Init() error { + return nil +} + +func (i CloudImporter) Sync() error { + return nil +} + func (i CloudImporter) exists(filename string) bool { fullURL := i.baseDAV fullURL.Path = path.Join(fullURL.Path, filename) diff --git a/admin/sync/importer_localfs.go b/admin/sync/importer_localfs.go index 515be07a..e088cbb6 100644 --- a/admin/sync/importer_localfs.go +++ b/admin/sync/importer_localfs.go @@ -2,6 +2,7 @@ package sync import ( "bufio" + "fmt" "io/ioutil" "os" "path" @@ -11,7 +12,7 @@ import ( // inside a local directory from your filesystem. type LocalImporter struct { // Base is the root directory used by the LocalImporter. It should contains all themes. - Base string + Base string // Symlink changes the normal file copy/concatenate behaviour to symlink/concatenate. // If enable, your base directory must be accessible by the frontend processus as it will follow the symlink. Symlink bool @@ -25,6 +26,23 @@ func (i LocalImporter) Kind() string { } } +func (i LocalImporter) Init() error { + if f, err := os.Stat(i.Base); os.IsNotExist(err) { + if err = os.Mkdir(i.Base, 0751); err != nil { + return err + } + } else if err != nil { + return err + } else if !f.IsDir() { + return fmt.Errorf("%q exists and is not a directory", i.Base) + } + return nil +} + +func (i LocalImporter) Sync() error { + return nil +} + func (i LocalImporter) exists(filename string) bool { _, err := os.Stat(i.toURL(filename)) return !os.IsNotExist(err) @@ -46,7 +64,7 @@ func (i LocalImporter) importFile(URI string, next func(string, string) (interfa os.Symlink(i.toURL(URI), dest) return next(dest, URI) } else { - os.Symlink(i.toURL(URI) + "_MERGED", dest) + os.Symlink(i.toURL(URI)+"_MERGED", dest) return ImportFile(i, URI, next) } } else { diff --git a/admin/sync/themes.go b/admin/sync/themes.go index 677c5229..4a44da4e 100644 --- a/admin/sync/themes.go +++ b/admin/sync/themes.go @@ -173,9 +173,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) { } // SyncThemes imports new or updates existing themes. -func SyncThemes(i Importer) []string { - var errs []string - +func SyncThemes(i Importer) (errs []string) { if themes, err := GetThemes(i); err != nil { errs = append(errs, err.Error()) } else { @@ -233,7 +231,7 @@ func SyncThemes(i Importer) []string { } } - return errs + return } // ApiListRemoteThemes is an accessor letting foreign packages to access remote themes list.