Repository navigation
feat: Allow File adapter to create file with specific locations or dynamic filenames #9557
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
base: alpha
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -218,4 +218,102 @@ describe('FilesController', () => { | |
| expect(gridFSAdapter.validateFilename(fileName)).not.toBe(null); | ||
| done(); | ||
| }); | ||
|
|
||
| it('should return filename and url when adapter returns both', async () => { | ||
| const config = Config.get(Parse.applicationId); | ||
| const adapterWithReturn = { ...mockAdapter }; | ||
| adapterWithReturn.createFile = () => { | ||
| return Promise.resolve({ | ||
| name: 'newFilename.txt', | ||
| url: 'http://example.com/newFilename.txt' | ||
| }); | ||
| }; | ||
| adapterWithReturn.getFileLocation = () => { | ||
| return Promise.resolve('http://example.com/file.txt'); | ||
| }; | ||
| const controllerWithReturn = new FilesController(adapterWithReturn, null, { preserveFileName: true }); | ||
|
|
||
| const result = await controllerWithReturn.createFile( | ||
| config, | ||
| 'originalFile.txt', | ||
| 'data', | ||
| 'text/plain' | ||
| ); | ||
|
|
||
| expect(result.name).toBe('newFilename.txt'); | ||
| expect(result.url).toBe('http://example.com/newFilename.txt'); | ||
| }); | ||
|
Comment on lines
+222
to
+245
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has since been addressed on the branch. Verified against the current branch: |
||
|
|
||
| it('should use original filename and generate url when adapter returns nothing', async () => { | ||
| const config = Config.get(Parse.applicationId); | ||
| const adapterWithoutReturn = { ...mockAdapter }; | ||
| adapterWithoutReturn.createFile = () => { | ||
| return Promise.resolve(); | ||
| }; | ||
| adapterWithoutReturn.getFileLocation = (config, filename) => { | ||
| return Promise.resolve(`http://example.com/${filename}`); | ||
| }; | ||
|
|
||
| const controllerWithoutReturn = new FilesController(adapterWithoutReturn, null, { preserveFileName: true }); | ||
| const result = await controllerWithoutReturn.createFile( | ||
| config, | ||
| 'originalFile.txt', | ||
| 'data', | ||
| 'text/plain', | ||
| {} | ||
| ); | ||
|
|
||
| expect(result.name).toBe('originalFile.txt'); | ||
| expect(result.url).toBe('http://example.com/originalFile.txt'); | ||
| }); | ||
|
|
||
| it('should use original filename when adapter returns only url', async () => { | ||
| const config = Config.get(Parse.applicationId); | ||
| const adapterWithOnlyURL = { ...mockAdapter }; | ||
| adapterWithOnlyURL.createFile = () => { | ||
| return Promise.resolve({ | ||
| url: 'http://example.com/partialFile.txt' | ||
| }); | ||
| }; | ||
| adapterWithOnlyURL.getFileLocation = () => { | ||
| return Promise.resolve('http://example.com/file.txt'); | ||
| }; | ||
|
|
||
| const controllerWithPartial = new FilesController(adapterWithOnlyURL, null, { preserveFileName: true }); | ||
| const result = await controllerWithPartial.createFile( | ||
| config, | ||
| 'originalFile.txt', | ||
| 'data', | ||
| 'text/plain', | ||
| {} | ||
| ); | ||
|
|
||
| expect(result.name).toBe('originalFile.txt'); | ||
| expect(result.url).toBe('http://example.com/partialFile.txt'); | ||
| }); | ||
|
|
||
| it('should use adapter filename and generate url when adapter returns only filename', async () => { | ||
| const config = Config.get(Parse.applicationId); | ||
| const adapterWithOnlyFilename = { ...mockAdapter }; | ||
| adapterWithOnlyFilename.createFile = () => { | ||
| return Promise.resolve({ | ||
| name: 'newname.txt' | ||
| }); | ||
| }; | ||
| adapterWithOnlyFilename.getFileLocation = (config, filename) => { | ||
| return Promise.resolve(`http://example.com/${filename}`); | ||
| }; | ||
|
|
||
| const controllerWithOnlyFilename = new FilesController(adapterWithOnlyFilename, null, { preserveFileName: true }); | ||
| const result = await controllerWithOnlyFilename.createFile( | ||
| config, | ||
| 'originalFile.txt', | ||
| 'data', | ||
| 'text/plain', | ||
| {} | ||
| ); | ||
|
|
||
| expect(result.name).toBe('newname.txt'); | ||
| expect(result.url).toBe('http://example.com/newname.txt'); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,11 +46,23 @@ export class FilesController extends AdaptableController { | |
| }); | ||
| } | ||
|
|
||
| const location = await this.adapter.getFileLocation(config, filename); | ||
| await this.adapter.createFile(filename, data, contentType, options); | ||
| // The adapter receives the server config so that it can derive a location | ||
| // itself, and may report back a filename it changed and a url it already | ||
| // resolved. An adapter that returns nothing keeps the previous behavior. | ||
| const createResult = await this.adapter.createFile( | ||
| filename, | ||
| data, | ||
| contentType, | ||
| options, | ||
| config | ||
| ); | ||
| // The location has to be resolved after creation, not before, because the | ||
| // adapter may have renamed the file. | ||
| const name = createResult?.name || filename; | ||
| const url = createResult?.url || (await this.adapter.getFileLocation(config, name)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '1,155p' src/Controllers/FilesController.js
sed -n '20,85p' src/Adapters/Files/FilesAdapter.js
rg -n 'getFileLocation\\(|async deleteFile\\(|createFile\\(' src/Adapters/Files spec/FilesController.spec.js | head -100Repository: parse-community/parse-server Length of output: 8388 🏁 Script executed: #!/bin/bash
set -u
printf '%s\n' '--- Adapter files ---'
rg --files src/Adapters/Files
printf '%s\n' '--- Adapter method locations ---'
rg -n 'createFile[[:space:]]*\\(|getFileLocation[[:space:]]*\\(|deleteFile[[:space:]]*\\(' src/Adapters/Files
printf '%s\n' '--- Adapter implementations ---'
for f in $(rg --files src/Adapters/Files -g '*.js' -g '*.ts'); do
if rg -q 'createFile[[:space:]]*\\(|getFileLocation[[:space:]]*\\(|deleteFile[[:space:]]*\\(' "$f"; then
printf '\n### %s\n' "$f"
rg -n -C 10 'createFile[[:space:]]*\\(|getFileLocation[[:space:]]*\\(|deleteFile[[:space:]]*\\(' "$f"
fi
done
printf '%s\n' '--- Controller history at supplied merge base ---'
git show 4ec73c57725d0157157e2a918ca518e5853c98ea:src/Controllers/FilesController.js | nl -ba | sed -n '20,78p'Repository: parse-community/parse-server Length of output: 3654 🏁 Script executed: #!/bin/bash
set -u
for f in src/Adapters/Files/GridStoreAdapter.js src/Adapters/Files/GridFSBucketAdapter.js; do
printf '\n--- %s (%s lines) ---\n' "$f" "$(wc -l < "$f")"
rg -n -F 'createFile(' "$f" || test "$?" -eq 1
rg -n -F 'getFileLocation(' "$f" || test "$?" -eq 1
rg -n -F 'deleteFile(' "$f" || test "$?" -eq 1
done
printf '\n--- GridStore relevant method bodies ---\n'
rg -n -C 35 -F 'createFile(' src/Adapters/Files/GridStoreAdapter.js
rg -n -C 18 -F 'getFileLocation(' src/Adapters/Files/GridStoreAdapter.js
rg -n -C 18 -F 'deleteFile(' src/Adapters/Files/GridStoreAdapter.js
printf '\n--- GridFSBucket relevant method bodies ---\n'
rg -n -C 35 -F 'createFile(' src/Adapters/Files/GridFSBucketAdapter.js
rg -n -C 18 -F 'getFileLocation(' src/Adapters/Files/GridFSBucketAdapter.js
rg -n -C 18 -F 'deleteFile(' src/Adapters/Files/GridFSBucketAdapter.jsRepository: parse-community/parse-server Length of output: 8270 🏁 Script executed: #!/bin/bash
set -u
printf '%s\n' '--- GridStoreAdapter.js ---'
nl -ba src/Adapters/Files/GridStoreAdapter.js
printf '%s\n' '--- Adapter binding ---'
rg -n -F 'FilesController' src/Controllers src
rg -n -F 'FilesAdapter' src/Controllers/AdaptableController.js src/Controllers/FilesController.js src/Adapters/Files/FilesAdapter.js
printf '%s\n' '--- Custom/built-in adapter docs and tests ---'
rg -n -i -C 3 'custom file adapter|file adapter|GridFSBucketAdapter|GridStoreAdapter|createFile\\(' docs README.md spec src/Adapters/Files src/Controllers/FilesController.js 2>/dev/null | head -240
printf '%s\n' '--- Controller test adapter contracts ---'
rg -n -F 'createFile(' spec/FilesController.spec.js
rg -n -F 'getFileLocation(' spec/FilesController.spec.js
rg -n -F 'new FilesController' spec/FilesController.spec.js
printf '%s\n' '--- PR diff for controller ---'
git diff 4ec73c57725d0157157e2a918ca518e5853c98ea c045a404d3dd8d1222a63d91f72c9467859d881e -- src/Controllers/FilesController.jsRepository: parse-community/parse-server Length of output: 4781 🏁 Script executed: #!/bin/bash
set -u
printf '%s\n' '--- AdaptableController ---'
nl -ba src/Controllers/AdaptableController.js | sed -n '1,130p'
printf '%s\n' '--- FilesController binding/configuration ---'
nl -ba src/Controllers/index.js | sed -n '75,108p'
printf '%s\n' '--- FilesController changed-result test implementations ---'
nl -ba spec/FilesController.spec.js | sed -n '215,330p'
printf '%s\n' '--- FilesAdapter public contract ---'
nl -ba src/Adapters/Files/FilesAdapter.js | sed -n '1,78p'Repository: parse-community/parse-server Length of output: 12518 Avoid storing a file before fallback URL resolution can fail. A custom 🤖 Prompt for AI Agents |
||
| return { | ||
| url: location, | ||
| name: filename, | ||
| url, | ||
| name, | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has since been addressed on the branch.
FilesController#createFilenow passesconfigas the fifth argument and consumes the adapter's return value, resolving the location after creation rather than before, so a filename the adapter changed is reflected in both the returned name and the URL. An adapter that returns nothing falls back to the previous behavior.Verified against the current branch:
spec/FilesController.spec.js,spec/ParseFile.spec.jsand theParse.Filespecs inspec/CloudCode.spec.jsall pass, including the assertion thatcreateFileis called withconfig.