Skip to content

Commit

Permalink
fix(import): allow noUpload with and without jpath or without field
Browse files Browse the repository at this point in the history
Fixes: #51
  • Loading branch information
targos committed Jan 26, 2017
1 parent 5d7ea97 commit 7fbd4da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
24 changes: 13 additions & 11 deletions src/couch/attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ const methods = {
return getAttachmentFromEntry(entry, this, name, asStream);
},

async addFileToJpath(id, user, jpath, json, file, newContent) {
async addFileToJpath(id, user, jpath, json, file, newContent, noFile) {
if (!Array.isArray(jpath)) {
throw new CouchError('jpath must be an array');
}
if (typeof json !== 'object') {
throw new CouchError('json must be an object');
}
if (typeof file !== 'object') {
if (typeof file !== 'object' || file === null) {
throw new CouchError('file must be an object');
}
if (!file.field || !file.name || !file.data) {
if (!noFile && !file.field || !file.name || !file.data) {
throw new CouchError('file must have field, name and data properties');
}

Expand Down Expand Up @@ -82,16 +82,18 @@ const methods = {
current.push(json);
}

json[file.field] = {
filename: file.name
};
if (!noFile) {
json[file.field] = {
filename: file.name
};

if (!entry._attachments) entry._attachments = {};
if (!entry._attachments) entry._attachments = {};

entry._attachments[file.name] = {
content_type: file.content_type,
data: (typeof file.data === 'string') ? file.data : file.data.toString('base64')
};
entry._attachments[file.name] = {
content_type: file.content_type,
data: (typeof file.data === 'string') ? file.data : file.data.toString('base64')
};
}
return this.insertEntry(entry, user);
}
};
Expand Down
16 changes: 8 additions & 8 deletions src/import/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,26 +147,24 @@ function fillInfoWithResult(info, result) {
error.skip = true;
throw error;
}
if (typeof result.jpath !== 'string') {
if (typeof result.jpath !== 'string' && typeof result.jpath !== 'undefined') {
throw new Error('parse: jpath must be a string');
}
if (typeof result.data !== 'object' || result.data === null) {
throw new Error('parse: data must be an object');
}
if (typeof result.field !== 'string') {
if (typeof result.field !== 'string' && typeof result.field !== 'undefined') {
throw new Error('parse: field must be a string');
}

debug.trace(`jpath: ${result.jpath}`);
info.jpath = result.jpath.split('.');
if (result.jpath) info.jpath = result.jpath.split('.');
info.data = result.data;
info.content_type = result.content_type || 'application/octet-stream';
info.field = result.field;
info.reference = result.reference;
info.content = result.content;
if (result.noUpload) {
info.noUpload = true;
}
info.noUpload = !!result.noUpload;
info.attachments = result.attachments;
}

Expand All @@ -182,8 +180,8 @@ async function checkDocumentExists(info, filename, contents, couch) {
async function updateDocument(info, docInfo, isParse, isJson, filename, contents, couch) {
debug.trace('updateDocument');
if (isParse) {
const joined = `${info.jpath.join('/')}/`;
if (!info.noUpload) {
const joined = info.jpath ? `${info.jpath.join('/')}/` : '';
if (!info.noUpload && info.jpath) {
const goodFilename = fold(filename, '_');
await couch.addFileToJpath(info.id, info.owner, info.jpath, info.data, {
field: info.field,
Expand All @@ -192,6 +190,8 @@ async function updateDocument(info, docInfo, isParse, isJson, filename, contents
data: contents,
content_type: info.content_type
}, info.content);
} else if (info.jpath) {
await couch.addFileToJpath(info.id, info.owner, info.jpath, info.data, {reference: info.reference}, info.content, true);
} else {
await updateContent();
}
Expand Down

0 comments on commit 7fbd4da

Please sign in to comment.