admin: localimporter can make symlink instead of copying whole files

This commit is contained in:
nemunaire 2017-12-12 07:13:38 +01:00 committed by Pierre-Olivier Mercier
parent d81f068eba
commit 8ed23ddc7a
5 changed files with 63 additions and 24 deletions

View file

@ -15,11 +15,11 @@ func SyncExerciceHints(i Importer, exercice fic.Exercice) []string {
} else if hints, err := i.listDir(path.Join(exercice.Path, "hints")); err != nil {
errs = append(errs, err.Error())
} else {
for _, hfile := range hints {
for n, hfile := range hints {
if hint_cnt, err := getFileContent(i, path.Join(exercice.Path, "hints", hfile)); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to read hint file %q: %s", path.Base(exercice.Path), hfile, err))
continue
} else if _, err := exercice.AddHint(hfile, hint_cnt, 1); err != nil {
} else if _, err := exercice.AddHint(fmt.Sprintf("Indice #%d", n + 1), hint_cnt, 1); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to add hint %q: %s", path.Base(exercice.Path), hfile, err))
continue
}

View file

@ -20,6 +20,7 @@ type Importer interface {
Kind() string
exists(filename string) bool
toURL(filename string) string
importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error)
getFile(filename string, writer *bufio.Writer) error
listDir(filename string) ([]string, error)
}
@ -83,10 +84,6 @@ func ImportFile(i Importer, URI string, next func(string, string) (interface{},
if r, err := next(dest, URI); err == nil {
return r, err
}
if err := os.Remove(dest); err != nil {
return nil, err
}
}
// Ensure no more file is registered with this path

View file

@ -10,10 +10,6 @@ import (
"github.com/studio-b12/gowebdav"
)
var CloudDAVBase string = "https://srs.epita.fr/owncloud/remote.php/webdav/FIC 2018"
var CloudUsername string = "fic"
var CloudPassword string = ""
type CloudImporter struct {
baseDAV url.URL
username string
@ -55,6 +51,10 @@ func (i CloudImporter) toURL(filename string) string {
return fullURL.String()
}
func (i CloudImporter) importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error) {
return ImportFile(i, URI, next)
}
func (i CloudImporter) getFile(filename string, writer *bufio.Writer) error {
fullURL := i.baseDAV
fullURL.Path = path.Join(fullURL.Path, filename)

View file

@ -8,15 +8,20 @@ import (
)
type LocalImporter struct {
Base string
Base string
Symlink bool
}
func (i LocalImporter) Kind() string {
return "local file importer: " + i.Base
if i.Symlink {
return "local file importer (through symlink): " + i.Base
} else {
return "local file importer: " + i.Base
}
}
func (i LocalImporter) exists(filename string) bool {
_, err := os.Stat(path.Join(i.Base, filename))
_, err := os.Stat(i.toURL(filename))
return !os.IsNotExist(err)
}
@ -24,6 +29,24 @@ func (i LocalImporter) toURL(filename string) string {
return path.Join(i.Base, filename)
}
func (i LocalImporter) importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error) {
if i.Symlink {
dest := getDestinationFilePath(URI)
if err := os.MkdirAll(path.Dir(dest), 0755); err != nil {
return nil, err
}
if i.exists(URI) {
os.Symlink(i.toURL(URI), dest)
} else {
os.Symlink(i.toURL(URI) + "_MERGED", dest)
}
}
return ImportFile(i, URI, next)
}
func (i LocalImporter) getFile(filename string, writer *bufio.Writer) error {
if fd, err := os.Open(path.Join(i.Base, filename)); err != nil {
return err