Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix issue when moving a file to the root directory #1377

Merged
merged 2 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/store/files/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,21 @@ export const actions: ActionTree<FileState, RootState> = {
break

case 'move_file':
if (payload.source_item?.path === 'printer_autosave.cfg' && payload.source_item?.root === 'config')
// just create a new printer_autosave.cfg file instead of rename the printer.cfg
if (payload.source_item?.path === 'printer_autosave.cfg' && payload.source_item?.root === 'config') {
commit('setCreateFile', payload)
else {
await commit('setMoveFile', payload)
if (
payload.item.root === 'gcodes' &&
validGcodeExtensions.includes(payload.item.path.slice(payload.item.path.lastIndexOf('.')))
) {
await dispatch('requestMetadata', {
filename: 'gcodes/' + payload.item.path,
})
}
return
}

// move all other files
await commit('setMoveFile', payload)
if (
payload.item.root === 'gcodes' &&
validGcodeExtensions.includes(payload.item.path.slice(payload.item.path.lastIndexOf('.')))
) {
await dispatch('requestMetadata', {
filename: 'gcodes/' + payload.item.path,
})
}
break

Expand Down
36 changes: 16 additions & 20 deletions src/store/files/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,40 +109,36 @@ export const mutations: MutationTree<FileState> = {

const lastSlashOld = payload.source_item.path.lastIndexOf('/')
if (lastSlashOld !== -1) {
filenameOld = payload.source_item.path.substr(lastSlashOld + 1)
pathnameOld = payload.source_item.root + '/' + payload.source_item.path.substr(0, lastSlashOld + 1)
filenameOld = payload.source_item.path.substring(lastSlashOld + 1)
pathnameOld = payload.source_item.root + '/' + payload.source_item.path.substring(0, lastSlashOld)
}

let filenameNew = payload.item.path
let pathnameNew = payload.item.root

const lastSlashNew = payload.item.path.lastIndexOf('/')
if (lastSlashNew !== -1) {
filenameNew = payload.item.path.substr(lastSlashNew + 1)
pathnameNew = payload.item.root + '/' + payload.item.path.substr(0, lastSlashNew + 1)
filenameNew = payload.item.path.substring(lastSlashNew + 1)
pathnameNew = payload.item.root + '/' + payload.item.path.substring(0, lastSlashNew)
}

const pathOld = findDirectory(state.filetree, pathnameOld.split('/'))
const indexFile = pathOld?.findIndex((element: FileStateFile) => element.filename === filenameOld)

if (indexFile && pathOld && pathOld[indexFile]) {
const file = pathOld.splice(indexFile, 1)[0]
file.filename = filenameNew

//cleanup thumbnails and force reload
if (
pathnameOld !== pathnameNew &&
'metadataPulled' in file &&
file.metadataPulled &&
'thumbnails' in file
) {
file.metadataPulled = false
delete file.thumbnails
}
// break if old file not found
if (indexFile === undefined || indexFile === -1 || pathOld === null) return

const newPath = findDirectory(state.filetree, pathnameNew.split('/'))
newPath?.push(file)
const file = pathOld.splice(indexFile, 1)[0]
file.filename = filenameNew

//cleanup thumbnails and force reload
if (pathnameOld !== pathnameNew && 'metadataPulled' in file && file.metadataPulled && 'thumbnails' in file) {
file.metadataPulled = false
delete file.thumbnails
}

const newPath = findDirectory(state.filetree, pathnameNew.split('/'))
newPath?.push(file)
},

setModifyFile(state, payload) {
Expand Down