Fix import file copying (#1085)

This commit is contained in:
InfiniteTF 2021-01-31 22:15:10 +01:00 committed by GitHub
parent 23d2668b38
commit 6114caa938
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 32 deletions

View File

@ -25,8 +25,12 @@ func (r *mutationResolver) MetadataImport(ctx context.Context) (string, error) {
} }
func (r *mutationResolver) ImportObjects(ctx context.Context, input models.ImportObjectsInput) (string, error) { func (r *mutationResolver) ImportObjects(ctx context.Context, input models.ImportObjectsInput) (string, error) {
t := manager.CreateImportTask(config.GetVideoFileNamingAlgorithm(), input) t, err := manager.CreateImportTask(config.GetVideoFileNamingAlgorithm(), input)
_, err := manager.GetInstance().RunSingleTask(t) if err != nil {
return "", err
}
_, err = manager.GetInstance().RunSingleTask(t)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -32,7 +32,7 @@ type ImportTask struct {
json jsonUtils json jsonUtils
BaseDir string BaseDir string
ZipFile io.Reader TmpZip string
Reset bool Reset bool
DuplicateBehaviour models.ImportDuplicateEnum DuplicateBehaviour models.ImportDuplicateEnum
MissingRefBehaviour models.ImportMissingRefEnum MissingRefBehaviour models.ImportMissingRefEnum
@ -42,15 +42,37 @@ type ImportTask struct {
fileNamingAlgorithm models.HashAlgorithm fileNamingAlgorithm models.HashAlgorithm
} }
func CreateImportTask(a models.HashAlgorithm, input models.ImportObjectsInput) *ImportTask { func CreateImportTask(a models.HashAlgorithm, input models.ImportObjectsInput) (*ImportTask, error) {
baseDir, err := instance.Paths.Generated.TempDir("import")
if err != nil {
logger.Errorf("error creating temporary directory for import: %s", err.Error())
return nil, err
}
tmpZip := ""
if input.File.File != nil {
tmpZip = filepath.Join(baseDir, "import.zip")
out, err := os.Create(tmpZip)
if err != nil {
return nil, err
}
_, err = io.Copy(out, input.File.File)
out.Close()
if err != nil {
return nil, err
}
}
return &ImportTask{ return &ImportTask{
txnManager: GetInstance().TxnManager, txnManager: GetInstance().TxnManager,
ZipFile: input.File.File, BaseDir: baseDir,
TmpZip: tmpZip,
Reset: false, Reset: false,
DuplicateBehaviour: input.DuplicateBehaviour, DuplicateBehaviour: input.DuplicateBehaviour,
MissingRefBehaviour: input.MissingRefBehaviour, MissingRefBehaviour: input.MissingRefBehaviour,
fileNamingAlgorithm: a, fileNamingAlgorithm: a,
} }, nil
} }
func (t *ImportTask) GetStatus() JobStatus { func (t *ImportTask) GetStatus() JobStatus {
@ -60,15 +82,7 @@ func (t *ImportTask) GetStatus() JobStatus {
func (t *ImportTask) Start(wg *sync.WaitGroup) { func (t *ImportTask) Start(wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
if t.ZipFile != nil { if t.TmpZip != "" {
// unzip the file and defer remove the temp directory
var err error
t.BaseDir, err = instance.Paths.Generated.TempDir("import")
if err != nil {
logger.Errorf("error creating temporary directory for import: %s", err.Error())
return
}
defer func() { defer func() {
err := utils.RemoveDir(t.BaseDir) err := utils.RemoveDir(t.BaseDir)
if err != nil { if err != nil {
@ -128,29 +142,15 @@ func (t *ImportTask) Start(wg *sync.WaitGroup) {
} }
func (t *ImportTask) unzipFile() error { func (t *ImportTask) unzipFile() error {
// copy the zip file to the temporary directory
tmpZip := filepath.Join(t.BaseDir, "import.zip")
out, err := os.Create(tmpZip)
if err != nil {
return err
}
if _, err := io.Copy(out, t.ZipFile); err != nil {
out.Close()
return err
}
out.Close()
defer func() { defer func() {
err := os.Remove(tmpZip) err := os.Remove(t.TmpZip)
if err != nil { if err != nil {
logger.Errorf("error removing temporary zip file %s: %s", tmpZip, err.Error()) logger.Errorf("error removing temporary zip file %s: %s", t.TmpZip, err.Error())
} }
}() }()
// now we can read the zip file // now we can read the zip file
r, err := zip.OpenReader(tmpZip) r, err := zip.OpenReader(t.TmpZip)
if err != nil { if err != nil {
return err return err
} }