Import: Fix for "invalid cross-device link" error #136

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2019-12-01 16:23:18 +01:00
parent 9d646a42bf
commit 895a1cf175
2 changed files with 42 additions and 7 deletions

View file

@ -433,15 +433,23 @@ func (m *MediaFile) HasSameFilename(other *MediaFile) bool {
return m.Filename() == other.Filename()
}
// Move a mediafile to a new file with the filename provided in parameter.
// Move file to a new destination with the filename provided in parameter.
func (m *MediaFile) Move(newFilename string) error {
if err := os.Rename(m.filename, newFilename); err != nil {
log.Debugf("could not rename file, falling back to copy and delete: %s", err.Error())
} else {
m.filename = newFilename
return nil
}
if err := m.Copy(newFilename); err != nil {
return err
}
m.filename = newFilename
return nil
return os.Remove(m.filename)
}
// Copy a mediafile to another file by destinationFilename.

View file

@ -492,19 +492,46 @@ func TestMediaFile_Exists(t *testing.T) {
assert.Nil(t, mediaFile)
}
func TestMediaFile_Move(t *testing.T) {
conf := config.TestConfig()
tmpPath := conf.CachePath() + "/_tmp/TestMediaFile_Move"
origName := tmpPath + "/original.jpg"
destName := tmpPath + "/destination.jpg"
os.MkdirAll(tmpPath, os.ModePerm)
defer os.RemoveAll(tmpPath)
f, err := NewMediaFile(conf.ExamplesPath() + "/table_white.jpg")
assert.Nil(t, err)
f.Copy(origName)
assert.True(t, util.Exists(origName))
m, err := NewMediaFile(origName)
assert.Nil(t, err)
if err = m.Move(destName); err != nil {
t.Errorf("failed to move: %s", err)
}
assert.True(t, util.Exists(destName))
assert.Equal(t, destName, m.Filename())
}
func TestMediaFile_Copy(t *testing.T) {
conf := config.TestConfig()
thumbsPath := conf.CachePath() + "/_tmp"
tmpPath := conf.CachePath() + "/_tmp/TestMediaFile_Copy"
os.MkdirAll(thumbsPath, os.ModePerm)
os.MkdirAll(tmpPath, os.ModePerm)
defer os.RemoveAll(thumbsPath)
defer os.RemoveAll(tmpPath)
mediaFile, err := NewMediaFile(conf.ExamplesPath() + "/table_white.jpg")
assert.Nil(t, err)
mediaFile.Copy(thumbsPath + "table_whitecopy.jpg")
assert.True(t, util.Exists(thumbsPath+"table_whitecopy.jpg"))
mediaFile.Copy(tmpPath + "table_whitecopy.jpg")
assert.True(t, util.Exists(tmpPath+"table_whitecopy.jpg"))
}
func TestMediaFile_Extension(t *testing.T) {