-
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into 395-xml-form-version
- Loading branch information
Showing
61 changed files
with
1,744 additions
and
334 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
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 fs = require('fs'); | ||
const { promisify } = require('util'); | ||
const path = require('path'); | ||
|
||
const db = require('../db'); | ||
const logger = require('../logger'); | ||
|
||
const DEFAULT_LOGO_PATH = path.join(__dirname, '..', 'resources', 'logo', 'medic-logo-light-full.svg'); | ||
|
||
const getInlineImage = (data, contentType) => `data:${contentType};base64,${data}`; | ||
|
||
const getBrandingDoc = async () => { | ||
try { | ||
return await db.medic.get('branding', { attachments: true }); | ||
} catch(e) { | ||
logger.warn('Could not find branding doc on CouchDB: %o', e); | ||
return; | ||
} | ||
}; | ||
|
||
const getName = (doc) => (doc && doc.title) || 'CHT'; | ||
|
||
const getLogo = async (doc) => { | ||
let data; | ||
let contentType; | ||
const name = doc && doc.resources && doc.resources.logo; | ||
if (name) { | ||
const image = doc._attachments[name]; | ||
data = image.data; | ||
contentType = image.content_type; | ||
} else { | ||
const logo = await promisify(fs.readFile)(DEFAULT_LOGO_PATH, {}); | ||
data = Buffer.from(logo).toString('base64'); | ||
contentType = 'image/svg+xml'; | ||
} | ||
return getInlineImage(data, contentType); | ||
}; | ||
|
||
const getIcon = (doc) => (doc && doc.resources && doc.resources.icon) || 'icon.png'; | ||
|
||
const getBranding = async () => { | ||
const doc = await getBrandingDoc(); | ||
const logo = await getLogo(doc); | ||
return { | ||
name: getName(doc), | ||
logo: logo, | ||
icon: getIcon(doc), | ||
doc: doc | ||
}; | ||
}; | ||
|
||
module.exports.get = getBranding; |
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,35 @@ | ||
const { promisify } = require('util'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const _ = require('lodash'); | ||
|
||
const environment = require('../environment'); | ||
const brandingService = require('./branding'); | ||
|
||
const EXTRACTED_RESOURCES_PATH = environment.getExtractedResourcesPath(); | ||
const MANIFEST_OUTPUT_PATH = path.join(EXTRACTED_RESOURCES_PATH, 'manifest.json'); | ||
const TEMPLATE_PATH = path.join(__dirname, '..', 'templates', 'manifest.json'); | ||
|
||
const writeJson = async (branding) => { | ||
const file = await promisify(fs.readFile)(TEMPLATE_PATH, { encoding: 'utf-8' }); | ||
const template = _.template(file); | ||
const json = template({ branding }); | ||
return await promisify(fs.writeFile)(MANIFEST_OUTPUT_PATH, json); | ||
}; | ||
|
||
const writeIcon = async (doc) => { | ||
const name = doc && doc.resources && doc.resources.icon; | ||
const attachment = name && doc._attachments[name]; | ||
if (attachment) { | ||
const contents = Buffer.from(attachment.data, 'base64'); | ||
const outputPath = path.join(EXTRACTED_RESOURCES_PATH, 'img', name); | ||
await promisify(fs.writeFile)(outputPath, contents); | ||
} | ||
}; | ||
|
||
module.exports.generate = async () => { | ||
const branding = await brandingService.get(); | ||
await writeJson(branding); | ||
await writeIcon(branding.doc); | ||
}; |
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
Oops, something went wrong.