-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(*): support deleting remote files
- Loading branch information
Showing
10 changed files
with
244 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const logger = require('../../support/logger').create('lib/provider/dropbox/dropbox-media-file'); | ||
const fs = require('fs'); | ||
|
||
class DropboxMediaFile { | ||
#dropboxService; | ||
#dropboxFile; | ||
#storagePath; | ||
#sourceFile; | ||
#deleted; | ||
|
||
constructor(dropboxService, dropboxFile, storagePath) { | ||
this.#dropboxService = dropboxService; | ||
this.#dropboxFile = dropboxFile; | ||
this.#storagePath = storagePath; | ||
this.#deleted = false; | ||
} | ||
|
||
get fileType() { | ||
return this.#dropboxFile.name.replace(/^.*\.([^\w]+)/, '$1'); | ||
} | ||
|
||
get sourceFile() { | ||
return this.#sourceFile; | ||
} | ||
|
||
get remoteFile() { | ||
return this.#dropboxFile.name; | ||
} | ||
|
||
async download() { | ||
if (this.#deleted) { | ||
throw new Error(`cannot download file that has been deleted: ${this.remoteFile}`); | ||
} else if (this.#sourceFile) { | ||
throw new Error(`already downloaded: ${this.#sourceFile}`); | ||
} else { | ||
logger.info(`downloading ${this.remoteFile}`); | ||
this.#sourceFile = await this.#dropboxService.downloadAndVerify(this.#dropboxFile, this.#storagePath); | ||
} | ||
} | ||
|
||
async delete() { | ||
if (this.#deleted) { | ||
throw new Error(`cannot delete file that has already been deleted: ${this.#sourceFile}`); | ||
} | ||
logger.info(`deleting remote file: ${this.remoteFile}`); | ||
await this.#dropboxService.delete(this.#dropboxFile); | ||
await fs.promises.unlink(this.#sourceFile); | ||
this.#deleted = true; | ||
} | ||
} | ||
|
||
module.exports = DropboxMediaFile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const DropboxMediaFile = require('./dropbox-media-file'); | ||
const { when } = require('jest-when'); | ||
|
||
const dropboxService = { | ||
downloadAndVerify: jest.fn() | ||
}; | ||
|
||
const TARGET_PATH = './target-path'; | ||
const DROPBOX_FILE_NAME = 'mockFile.jpg'; | ||
const DROPBOX_FILE = { | ||
path_lower: `/mockFolder/${DROPBOX_FILE_NAME}`, | ||
name: DROPBOX_FILE_NAME | ||
}; | ||
|
||
describe('DropboxMediaFile', () => { | ||
let mediaFile; | ||
beforeEach(() => { | ||
mediaFile = new DropboxMediaFile(dropboxService, DROPBOX_FILE, TARGET_PATH); | ||
}); | ||
|
||
describe('download', () => { | ||
test('file downloads successfully', async () => { | ||
when(dropboxService.downloadAndVerify) | ||
.calledWith(DROPBOX_FILE, TARGET_PATH) | ||
.mockResolvedValue(`${TARGET_PATH}/${DROPBOX_FILE_NAME}`); | ||
|
||
await mediaFile.download(); | ||
expect(mediaFile.sourceFile).toBe(`${TARGET_PATH}/${DROPBOX_FILE_NAME}`); | ||
}); | ||
|
||
test('file can only be downloaded once', async () => { | ||
when(dropboxService.downloadAndVerify) | ||
.calledWith(DROPBOX_FILE, TARGET_PATH) | ||
.mockResolvedValue(`${TARGET_PATH}/${DROPBOX_FILE_NAME}`); | ||
|
||
await mediaFile.download(); | ||
|
||
expect(mediaFile.sourceFile).toBe(`${TARGET_PATH}/${DROPBOX_FILE_NAME}`); | ||
expect(mediaFile.download()).rejects.toThrow(`already downloaded: ${TARGET_PATH}/${DROPBOX_FILE_NAME}`); | ||
}); | ||
|
||
test.todo('file cannot be downloaded after it has been deleted'); | ||
}); | ||
|
||
describe('delete', () => { | ||
test.todo('file is deleted successfully'); | ||
test.todo('file cannot be deleted if it has already been deleted'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,47 @@ | ||
const DropboxProvider = require('./dropbox-provider'); | ||
const DropboxMediaFile = require('./dropbox-media-file'); | ||
const { when } = require('jest-when'); | ||
|
||
const dropboxService = { | ||
listFiles: jest.fn(), | ||
downloadAndVerify: jest.fn() | ||
}; | ||
|
||
const SOURCE_PATH = './source-path'; | ||
const SOURCE_PATH = '/source-path'; | ||
const TARGET_PATH = './target-path'; | ||
const DROPBOX_FILE_NAME = 'mockFile.jpg'; | ||
const DROPBOX_FILE = { | ||
'.tag': 'file', | ||
path_lower: DROPBOX_FILE_NAME | ||
}; | ||
|
||
const dropboxProvider = new DropboxProvider(dropboxService, SOURCE_PATH, TARGET_PATH); | ||
function dropboxFile(name, tag) { | ||
return { | ||
'.tag': tag || 'file', | ||
name: name | ||
}; | ||
} | ||
|
||
test('DropboxProvider list files', async (done) => { | ||
when(dropboxService.listFiles) | ||
.calledWith(SOURCE_PATH) | ||
.mockResolvedValue({ | ||
entries: [DROPBOX_FILE] | ||
}); | ||
function dropboxFolder(name) { | ||
return dropboxFile(name, 'folder'); | ||
} | ||
|
||
const files = await dropboxProvider.list(); | ||
expect(files).toEqual(expect.arrayContaining([DROPBOX_FILE])); | ||
done(); | ||
}); | ||
const dropboxProvider = new DropboxProvider(dropboxService, SOURCE_PATH, TARGET_PATH); | ||
|
||
test('DropboxProvider download file', async (done) => { | ||
when(dropboxService.downloadAndVerify) | ||
.calledWith(DROPBOX_FILE, TARGET_PATH) | ||
.mockResolvedValue(`${TARGET_PATH}/${DROPBOX_FILE_NAME}`); | ||
describe('DropboxProvider', () => { | ||
describe('list', () => { | ||
test('only include files (no folders)', async () => { | ||
when(dropboxService.listFiles) | ||
.calledWith(SOURCE_PATH) | ||
.mockResolvedValue({ | ||
entries: [ | ||
dropboxFile('a.jpg'), | ||
dropboxFile('b.jpg'), | ||
dropboxFolder('folderA'), | ||
dropboxFile('c.jpg'), | ||
dropboxFolder('folderB') | ||
] | ||
}); | ||
|
||
const filename = await dropboxProvider.download(DROPBOX_FILE); | ||
expect(filename).toBe(`${TARGET_PATH}/${DROPBOX_FILE_NAME}`); | ||
done(); | ||
const files = await dropboxProvider.list(); | ||
expect(files).toHaveLength(3); | ||
files.forEach((file) => expect(file).toBeInstanceOf(DropboxMediaFile)); | ||
expect(files).toMatchObject([{ remoteFile: 'a.jpg' }, { remoteFile: 'b.jpg' }, { remoteFile: 'c.jpg' }]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.