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

30 create oih delete action #33

Merged
merged 21 commits into from
Nov 27, 2019
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Apply code suggestions
denyshld committed Nov 26, 2019
commit 03c6622614edb7098a7d7c4b70898fe7db4dffb5
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ The following configuration fields are available:

Input metadata:

- **Filename**: Custom name for uploadad file.
- **Filename**: Custom name for uploaded file.

## Known limitations

32 changes: 24 additions & 8 deletions lib/actions/upload.js
Original file line number Diff line number Diff line change
@@ -19,10 +19,8 @@ exports.process = async function processAction(msg, cfg) {
results: [],
};
const dir = cfg.directory || '/';
// eslint-disable-next-line no-nested-ternary
const filename = msg.body.filename
? (Object.keys(msg.attachments).length > 1 ? msg.body.filename.split('.')[0] : msg.body.filename)
: null;
// eslint-disable-next-line no-use-before-define
const filename = prepareFilename(msg);
this.logger.info(`Prepared filename: ${filename}`);

const isExists = await sftp.exists(dir);
@@ -35,10 +33,8 @@ exports.process = async function processAction(msg, cfg) {
for (const key of Object.keys(msg.attachments)) {
const attachment = msg.attachments[key];
const cur = await sftp.cwd();
// eslint-disable-next-line no-nested-ternary
const keyName = filename
? (Object.keys(msg.attachments).length > 1 ? `${filename}_${key}` : filename)
: key;
// eslint-disable-next-line no-use-before-define
const keyName = prepareKeyname(key, filename, msg);
const targetPath = (cur.charAt(0) === '/') ? path.posix.resolve(dir, keyName) : path.resolve(dir, keyName);
this.logger.info(`Writing attachment to ${targetPath}`);

@@ -57,3 +53,23 @@ exports.process = async function processAction(msg, cfg) {
await sftp.end();
return eioUtils.newMessageWithBody(result);
};

function prepareFilename(msg) {
if (msg.body.filename) {
if (Object.keys(msg.attachments).length > 1) {
return msg.body.filename.split('.')[0];
}
return msg.body.filename;
}
return null;
}

function prepareKeyname(key, filename, msg) {
if (filename) {
if (Object.keys(msg.attachments).length > 1) {
return `${filename}_${key}`;
}
return filename;
}
return key;
}
39 changes: 39 additions & 0 deletions spec-integration/integration.spec.js
Original file line number Diff line number Diff line change
@@ -116,6 +116,45 @@ describe('SFTP integration test - upload then download', function () {
await sftp.rmdir(cfg.directory, false);
});

it('Uploads and reads attachments with custom name', async () => {
const cfg = {
host,
username,
password,
port,
directory,
};
sftp = new Sftp(bunyan.createLogger({ name: 'dummy' }), cfg);
await sftp.connect();

await upload.process.call(new TestEmitter(), {
body: {},
attachments: {
'logo.svg': {
url: 'https://app.elastic.io/img/logo.svg',
},
'logo2.svg': {
url: 'https://app.elastic.io/img/logo.svg',
},
},
}, cfg);

const receiver = new TestEmitter();
const msg = { body: { filename: 'custom.svg' } };
await read.process.call(receiver, msg, cfg);
expect(receiver.data.length).to.equal(2);
expect(receiver.data[0].body.filename).to.equal('custom_logo.svg');
expect(receiver.data[0].body.size).to.equal(4379);
expect(receiver.data[1].body.filename).to.equal('custom_logo2.svg');
expect(receiver.data[1].body.size).to.equal(4379);
const logoFilename = (await sftp.list(`${cfg.directory}${PROCESSED_FOLDER_NAME}`))[0].name;
const logo2Filename = (await sftp.list(`${cfg.directory}${PROCESSED_FOLDER_NAME}`))[1].name;
await sftp.delete(`${cfg.directory}${PROCESSED_FOLDER_NAME}/${logoFilename}`);
await sftp.delete(`${cfg.directory}${PROCESSED_FOLDER_NAME}/${logo2Filename}`);
await sftp.rmdir(`${cfg.directory}${PROCESSED_FOLDER_NAME}`, false);
await sftp.rmdir(cfg.directory, false);
});

it('Uploads, reads, and filters files by pattern match', async () => {
const cfg = {
host,