This repository has been archived by the owner on Nov 23, 2018. It is now read-only.
-
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.
Setting up spreadsheet CMS for 2019.
- Loading branch information
Showing
16 changed files
with
979 additions
and
12 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 was deleted.
Oops, something went wrong.
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,86 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const credentialsCache = {}; | ||
|
||
function getCredentialsPath() { | ||
return ( | ||
process.env.WWWTF_CREDENTIALS_PATH || | ||
path.resolve(process.env.HOME, '.wwwtf18') | ||
); | ||
} | ||
|
||
/** | ||
* Loads a private credentials-file. | ||
* Files with sensitive information are stored in the directory ~/.wwwtf18 | ||
* in JSON-Format. An alternative directory can be specified using the | ||
* env-variable `WWWTF_CREDENTIALS_PATH`, but that is mostly intended | ||
* to be used for CI/CD-servers and similar environments. | ||
* @param {string} filename json-filename without any path. | ||
* @param {boolean} ignoreMissing true to ignore missing files and return | ||
* no content. | ||
* @returns {*} the parsed content of the json-file | ||
*/ | ||
function loadCredentials(filename, ignoreMissing = false) { | ||
if (!credentialsCache[filename]) { | ||
const credentialsFile = path.resolve(getCredentialsPath(), filename); | ||
|
||
if (ignoreMissing && !fs.existsSync(credentialsFile)) { | ||
return null; | ||
} | ||
|
||
try { | ||
credentialsCache[filename] = require(credentialsFile); | ||
} catch (err) { | ||
console.error( | ||
"🔐 It appears that you don't have your credentials setup yet.\n" + | ||
' Please copy the file ' + | ||
filename + | ||
' to\n ' + | ||
getCredentialsPath() + | ||
'\n to continue. Ask your coworkers if you never heard of that file.' | ||
); | ||
|
||
throw new Error(`failed to load ${credentialsFile}: ${err}`); | ||
} | ||
} | ||
|
||
return credentialsCache[filename]; | ||
} | ||
|
||
/** | ||
* Checks if credentials with the given filename exist. | ||
* @param {string} filename file basename | ||
* @return {boolean} - | ||
*/ | ||
function hasCredentials(filename) { | ||
const credentialsFile = path.resolve(getCredentialsPath(), filename); | ||
|
||
return fs.existsSync(credentialsFile); | ||
} | ||
|
||
/** | ||
* Stores credentials to a file in the credentials-store. | ||
* @param {string} filename the file basename. | ||
* @param {object} data the data to store. | ||
*/ | ||
function storeCredentials(filename, data) { | ||
credentialsCache[filename] = data; | ||
|
||
const credentialsFile = path.resolve(getCredentialsPath(), filename); | ||
try { | ||
fs.writeFileSync(credentialsFile, JSON.stringify(data)); | ||
} catch (err) { | ||
console.error( | ||
`🔐 Failed to write credentials to file ${credentialsFile}.` | ||
); | ||
|
||
throw new Error(`failed to write credentials: ${err}`); | ||
} | ||
} | ||
|
||
module.exports = { | ||
getCredentialsPath, | ||
loadCredentials, | ||
hasCredentials, | ||
storeCredentials | ||
}; |
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,84 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const fetch = require('node-fetch'); | ||
const chalk = require('chalk'); | ||
const imageType = require('image-type'); | ||
const imageSize = require('image-size'); | ||
|
||
function getImageFilename(speaker, ext) { | ||
let filename = speaker.firstname + '-' + speaker.lastname; | ||
filename = filename.replace(/[^\w]/g, '-'); | ||
filename = filename.replace(/--/g, '-').toLowerCase(); | ||
|
||
return filename + '.' + ext; | ||
} | ||
|
||
function getLocalSpeakerImage(imagePath, speaker) { | ||
if (!imagePath) { | ||
return null; | ||
} | ||
|
||
const filename = getImageFilename(speaker, 'jpg'); | ||
const srcFilename = path.join(imagePath, filename); | ||
const destFilename = path.join('contents/images/speaker', filename); | ||
|
||
if (fs.existsSync(srcFilename)) { | ||
console.log(` --> image found in image-path:`, filename); | ||
const buffer = fs.readFileSync(srcFilename); | ||
const size = imageSize(buffer); | ||
fs.writeFileSync(destFilename, buffer); | ||
|
||
return { | ||
filename, | ||
width: size.width, | ||
height: size.height | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
async function downloadSpeakerImage(speaker) { | ||
const url = speaker.potraitImageUrl; | ||
console.log('downloadImage', url); | ||
if (!url) { | ||
console.error(chalk.yellow('no image specified for ' + speaker.id)); | ||
return {}; | ||
} | ||
|
||
try { | ||
const res = await fetch(url); | ||
|
||
if (!res.headers.get('content-type').startsWith('image')) { | ||
console.error(chalk.red.bold(' !!! url is not an image', url)); | ||
return {}; | ||
} | ||
|
||
const buffer = await res.buffer(); | ||
|
||
const info = imageType(buffer); | ||
if (!info) { | ||
console.error(chalk.red.bold(' !!! no type-imformation for image', url)); | ||
return {}; | ||
} | ||
|
||
const size = imageSize(buffer); | ||
const filename = getImageFilename(speaker, info.ext); | ||
const fullPath = 'contents/images/speaker/' + filename; | ||
|
||
console.info(' --> image downloaded ', chalk.green(fullPath)); | ||
fs.writeFileSync(fullPath, buffer); | ||
|
||
return { | ||
filename, | ||
width: size.width, | ||
height: size.height | ||
}; | ||
} catch (err) { | ||
console.error(chalk.red.bold(' !!! failed to download', url)); | ||
console.error(err); | ||
return {}; | ||
} | ||
} | ||
|
||
module.exports = {getLocalSpeakerImage, downloadSpeakerImage}; |
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,61 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const fetch = require('node-fetch'); | ||
const chalk = require('chalk'); | ||
|
||
function getLocalSponsorImage(imagePath, sponsor) { | ||
if (!imagePath) { | ||
return null; | ||
} | ||
|
||
const filename = sponsor.id + '.svg'; | ||
const srcFilename = path.join(imagePath, filename); | ||
const destFilename = path.join('contents/images/sponsor', filename); | ||
|
||
if (fs.existsSync(srcFilename)) { | ||
console.log(` --> image found in image-path:`, filename); | ||
const buffer = fs.readFileSync(srcFilename); | ||
fs.writeFileSync(destFilename, buffer); | ||
|
||
return { | ||
filename | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
async function downloadSponsorImage(sponsor) { | ||
const url = sponsor.logoUrl; | ||
console.log('downloadImage', url); | ||
if (!url) { | ||
console.error(chalk.yellow('no image specified for ' + sponsor.id)); | ||
return {}; | ||
} | ||
|
||
try { | ||
const res = await fetch(url); | ||
|
||
if (!res.headers.get('content-type').startsWith('image')) { | ||
console.error(chalk.red.bold(' !!! url is not an image', url)); | ||
return {}; | ||
} | ||
|
||
const buffer = await res.buffer(); | ||
const filename = sponsor.id + '.svg'; | ||
const fullPath = `contents/images/sponsor/${filename}`; | ||
|
||
console.info(' --> image downloaded ', chalk.green(fullPath)); | ||
fs.writeFileSync(fullPath, buffer); | ||
|
||
return { | ||
filename | ||
}; | ||
} catch (err) { | ||
console.error(chalk.red.bold(' !!! failed to download', url)); | ||
console.error(err); | ||
return {}; | ||
} | ||
} | ||
|
||
module.exports = {getLocalSponsorImage, downloadSponsorImage}; |
Oops, something went wrong.