Skip to content

Commit

Permalink
Add support for icon uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed May 28, 2017
1 parent a7f2df4 commit 328ef76
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 35 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ medic-config lg-uganda http://admin:pass@localhost:5984

# TODO

* support icon uploads
* support form XSL -> XML conversion
5 changes: 5 additions & 0 deletions bin/medic-conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const uploadAppSettings = require('../src/upload-app-settings');
const backupForms = require('../src/backup-forms');
const deleteForms = require('../src/delete-forms');
const uploadForms = require('../src/upload-forms');
const uploadResources = require('../src/upload-resources');

if(process.argv.length < 4) {
usage();
Expand Down Expand Up @@ -48,6 +49,10 @@ Promise.resolve()
.then(() => uploadForms(project, couchUrl))
.then(() => big_log('Form upload complete.'))

.then(() => big_log('Uploading resources...'))
.then(() => uploadResources(project, couchUrl))
.then(() => big_log('Resources upload complete.'))

.then(() => big_log('Project configuration upload complete.'))

.catch(console.log);
31 changes: 31 additions & 0 deletions src/attachments-from-dir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fs = require('./sync-fs');

module.exports = (dir, ...ignore) => {
const attachments = {};
fs.readdir(dir)
.filter(name => !ignore || !ignore.includes(name))
.forEach(fileName => {
const filePath = `${dir}/${fileName}`;
const data = fs.readBinary(filePath);
const mime = mimeTypeFor(fileName);
attachments[fileName] = {
content_type: mime,
data: new Buffer(data),
};
});
return attachments;
};

function mimeTypeFor(fileName) {
const extensionStart = fileName.indexOf('.');
const extension = extensionStart === -1 ?
fileName :
fileName.substring(extensionStart+1);

switch(extension) {
case 'json': return 'application/json';
case 'png' : return 'image/png';
case 'xml' : return 'application/xml';
default: throw new Error(`Unrecongised file extension: ${extension}`);
}
}
39 changes: 5 additions & 34 deletions src/upload-forms.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('./sync-fs');
const attachmentsFromDir = require('./attachments-from-dir');

const PouchDB = require('pouchdb');

Expand All @@ -25,46 +26,16 @@ module.exports = (project, couchUrl) => {
doc.context = properties.context;
doc.icon = properties.icon;
if(properties.internalId) {
console.log('WARN', 'DEPRECATED', 'Please do not manually set internalId in properties.json for new projects. Support for configuring this value will be dropped. Please see https://github.com/medic/medic-webapp/issues/3342.');
console.log('WARN', 'DEPRECATED', 'Form:', name, 'Please do not manually set internalId in properties.json for new projects. Support for configuring this value will be dropped. Please see https://github.com/medic/medic-webapp/issues/3342.');
doc.internalId = properties.internalId;
}
}

const attachments = {};
fs.readdir(`${formDir}`)
.filter(name => name !== 'properties.json')
.forEach(fileName => {
const filePath = `${formDir}/${fileName}`;
const data = fs.readBinary(filePath);
const mime = mimeTypeFor(fileName);
attachments[fileName] = {
content_type: mime,
data: new Buffer(data),
};
const xml = fs.read(`${formDir}/xml`);
doc.title = xml.substring(xml.indexOf('<h:title>') + 9, xml.indexOf('</h:title>'));

if(fileName === 'xml') {
// FIXME this is not how to parse XML
const xml = fs.read(filePath);
const title = xml.substring(xml.indexOf('<h:title>') + 9, xml.indexOf('</h:title>'));
doc.title = title;
}
});
doc._attachments = attachments;
doc._attachments = attachmentsFromDir(formDir);

return db.put(doc);
}));
};

function mimeTypeFor(fileName) {
const extensionStart = fileName.indexOf('.');
const extension = extensionStart === -1 ?
fileName :
fileName.substring(extensionStart+1);

switch(extension) {
case 'json': return 'application/json';
case 'png' : return 'image/png';
case 'xml' : return 'application/xml';
default: throw new Error(`Unrecongised file extension: ${extension}`);
}
}
19 changes: 19 additions & 0 deletions src/upload-resources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require('./sync-fs');
const attachmentsFromDir = require('./attachments-from-dir');

const PouchDB = require('pouchdb');

module.exports = (project, couchUrl) => {
const db = new PouchDB(couchUrl);

return db.get('resources')
.catch(e => {
if(e.status === 404) return { _id:'resources' };
else throw e;
})
.then(doc => {
doc.resources = fs.readJson(`${project}/resources.json`);
doc._attachments = attachmentsFromDir(`${project}/resources`);
return db.put(doc);
});
};

0 comments on commit 328ef76

Please sign in to comment.