Skip to content

Commit

Permalink
move existing directory
Browse files Browse the repository at this point in the history
  • Loading branch information
fiskus committed Jun 12, 2024
1 parent 5dfc783 commit b0f93c0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
40 changes: 36 additions & 4 deletions catalog/app/containers/Bucket/PackageDialog/FilesState.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ describe('utils/object', () => {
}
const fileAA = { name: 'foo/bar.txt' } as FileWithHash
const fileEA = { physicalKey: 's3://b/a' } as Model.PackageEntry
const fileEAConvertedToAdded = {
bucket: 'b',
key: 'a',
}

describe('Add', () => {
it('adds the file', () => {
Expand Down Expand Up @@ -150,10 +154,7 @@ describe('utils/object', () => {
const result = handleFilesAction(action, initial)(state)
expect(result).toMatchObject({
added: {
'lorem/ipsum/foo.txt': {
bucket: 'b',
key: 'a',
},
'lorem/ipsum/foo.txt': fileEAConvertedToAdded,
},
})
})
Expand Down Expand Up @@ -188,6 +189,37 @@ describe('utils/object', () => {
},
})
})

it('Move existing dir', () => {
const source = FilesEntry.Dir({
name: 'foo/bar/',
state: 'unchanged',
childEntries: [],
})
const dest = FilesEntry.Dir({
name: 'lorem/ipsum/',
state: 'unchanged',
childEntries: [],
})
const action = FilesAction.Move({
source: [source, 'root/inside/'],
dest: [dest],
})
const state = {
...emptyState,
existing: {
'root/inside/foo/bar/a.txt': fileEA,
'root/inside/foo/bar/b.txt': fileEA,
},
}
const result = handleFilesAction(action, initial)(state)
expect(result).toMatchObject({
added: {
'lorem/ipsum/bar/a.txt': fileEAConvertedToAdded,
'lorem/ipsum/bar/b.txt': fileEAConvertedToAdded,
},
})
})
})
})
})
27 changes: 22 additions & 5 deletions catalog/app/containers/Bucket/PackageDialog/FilesState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export function renameKey<T = Types.Json>(

// Moves all `foo/bar/*` keys to `foo/baz/*` as `/foo/baz/bar/*`
export function renameKeys<T = Types.Json>(
fromPrefix: string,
sourcePrefix: string,
destPath: string,
obj: Record<string, T>,
) {
return Object.entries(obj).reduce((acc, [key, value]) => {
if (!key.startsWith(fromPrefix)) return { ...acc, [key]: value }
if (!key.startsWith(sourcePrefix)) return { ...acc, [key]: value }
const newKey = key.replace(
s3paths.ensureSlash(fromPrefix),
s3paths.ensureSlash(join(destPath, basename(fromPrefix))),
s3paths.ensureSlash(sourcePrefix),
s3paths.ensureSlash(join(destPath, basename(sourcePrefix))),
)
return { ...acc, [newKey]: value }
}, {})
Expand Down Expand Up @@ -62,6 +62,23 @@ export function moveExistingToAdded(
}
}

export function moveExistingDirectoryToAdded(
sourcePath: string,
destPath: string,
state: FilesState,
) {
return Object.entries(state.existing).reduce((acc, [key, value]) => {
if (!key.startsWith(sourcePath)) return acc
const newKey = key.replace(
s3paths.ensureSlash(sourcePath),
s3paths.ensureSlash(join(destPath, basename(sourcePath))),
)
const existing = R.dissoc(key, acc.existing)
const added = R.assoc(newKey, packageEntryToS3File(value), acc.added)
return { ...acc, added, existing }
}, state)
}

export interface FileWithHash extends File {
hash: {
ready: boolean
Expand Down Expand Up @@ -243,7 +260,7 @@ export const handleFilesAction = FilesAction.match<
? `${destPrefix}${destDir.value.name}`

Check warning on line 260 in catalog/app/containers/Bucket/PackageDialog/FilesState.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Bucket/PackageDialog/FilesState.ts#L260

Added line #L260 was not covered by tests
: `${destDir.value.name}`
if (hasDir(sourcePath, state.existing)) {
throw new Error('Cannot transfer a directory')
return moveExistingDirectoryToAdded(sourcePath, destPath, state)
}
if (hasDir(sourcePath, state.added)) {
return {
Expand Down

0 comments on commit b0f93c0

Please sign in to comment.