Files
museum-digital-webdav-uploader/src/webdavupload/CheckRemoteContents.go
Joshua Ramon Enslin 7cfd3bb1de First working version
- Move from separate dirs for upload to a unified one (identify
  media/metadata files by file extension)
- Prevent uploading when an import is already scheduled
- Allow setting custom, parser-specific settings
- Add CLI
- Implement WebDAV upload
- Implement checking of upload folders for uploadable contents

Close #6, close #7, close #9, close #3, close #1, close #4
2025-02-27 17:29:20 +01:00

116 lines
3.1 KiB
Go

package webdavupload
import (
"os"
"sync"
"time"
"github.com/studio-b12/gowebdav"
"gitea.armuli.eu/museum-digital/museum-digital-webdav-uploader/src/configloader"
)
// Takes a path on the webdav remote and reads folder contents from it.
func listWebDavFolderContents(config configloader.MDWebDavUploaderConfig, path string) ([]os.FileInfo, error) {
c := gowebdav.NewClient(getWebDavHost(config), config.Username, config.WebDavAuthToken)
c.Connect()
files, err := c.ReadDir(path)
return files, err
}
// Returns a list of the files on the top level of the remote.
func ListTopLevelContents(config configloader.MDWebDavUploaderConfig) ([]os.FileInfo, error) {
return listWebDavFolderContents(config, ".")
}
// Returns a list of the files in the current metadata dir of the remote.
func ListMetadataDir(config configloader.MDWebDavUploaderConfig) ([]os.FileInfo, error) {
return listWebDavFolderContents(config, "./IMPORT_XML")
}
// Returns a list of the files in the current media dir of the remote.
func ListMediaDir(config configloader.MDWebDavUploaderConfig) ([]os.FileInfo, error) {
return listWebDavFolderContents(config, "./IMPORT_IMG")
}
func checkImportConfigExists(c *gowebdav.Client) bool {
_, err := c.Stat("./import_config.txt")
if gowebdav.IsErrNotFound(err) == true {
return false
}
return true
}
// Checks if a remote directory is free for updating:
// This means it can be read, and it does not contain files more recent than
// 10 minutes.
func checkRemoteDirIsFree(c *gowebdav.Client, path string) bool {
now := time.Now()
// Check metadata dir
files, err := c.ReadDir(path)
if err != nil {
print("Failed to load remote contents")
return false
}
for _, f := range(files) {
if diff := now.Sub(f.ModTime()); diff < 10 * time.Minute {
print("File " + f.Name() + " has been recently renamed")
return false
}
}
return true
}
// Checks if either the metadata or the image folder contains files that have
// been changed in the last 10 minutes.
// In this case, it can be assumed that there is a concurrent upload taking place.
func checkRemoteImportFilesAreTooRecent(c *gowebdav.Client) bool {
var wg sync.WaitGroup
var metadataDirFree bool
var mediaDirFree bool
wg.Add(2)
go func() {
defer wg.Done()
metadataDirFree = checkRemoteDirIsFree(c, "./IMPORT_XML")
}()
go func() {
defer wg.Done()
mediaDirFree = checkRemoteDirIsFree(c, "./IMPORT_IMG")
}()
wg.Wait()
if metadataDirFree == true && mediaDirFree == true {
return false
}
return true
}
// Checks the remote for the existence of an import_config.txt.
func CheckRemoteIsFree(c *gowebdav.Client) bool {
if checkImportConfigExists(c) == true {
return false
}
// Check if the remote media and metadata directories currently
// contain very recent files indicating a concurrent upload.
if checkRemoteImportFilesAreTooRecent(c) == true {
return false
}
return true
}