Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.then(responses => {
const images = [];
for (let i=0;i 0 && posX + avatarWidth + margin > imageWidth) {
posY += (avatarHeight + margin);
posX = margin;
}
const image = `<img xlink:href="data:${contentType};base64,${base64data}" height="${avatarHeight}" width="${avatarWidth}" y="${posY}" x="${posX}">`;
const imageLink = `<a id="${user.username}" xlink:href="${website.replace(/&/g,'&')}">${image}</a>`;
images.push(imageLink);
posX += avatarWidth + margin;
}
_transform(chunk, encoding, callback) {
this.length += chunk.length
if (!this.done) {
if (this.limit && this.length >= this.limit) {
this.error = new Error('Unable do determine image size: Limit reached')
this.done = true
} else {
this.buffer = this.buffer
? Buffer.concat([this.buffer, chunk])
: chunk
try {
const size = imageSize(this.buffer)
this.error = null
this.done = true
this.emit('size', size)
} catch (err) {
// Do not emit the error right away. Instead keep trying when new
// data is received until either the image size can be determined,
// or the end of the stream is reached and `_flush()` is called.
this.error = err
}
}
}
callback(null, chunk)
}
if (!responses[i]) continue;
const { headers } = responses[i][0];
const rawData = responses[i][1];
const user = users[i];
if (!user) continue;
const contentType = headers['content-type'];
const website =
options.linkToProfile || !user.website
? `${WEBSITE_URL}/${user.slug}`
: user.website;
const base64data = Buffer.from(rawData).toString('base64');
let avatarWidth = avatarHeight;
try {
// We make sure the image loaded properly
const dimensions = sizeOf(rawData);
avatarWidth = Math.round(
(dimensions.width / dimensions.height) * avatarHeight,
);
} catch (e) {
// Otherwise, we skip it
logger.warn(
'Cannot get the dimensions of the avatar of %s.',
user.slug,
{ image: user.image },
);
continue;
}
if (imageWidth > 0 && posX + avatarWidth + margin > imageWidth) {
posY += avatarHeight + margin;
posX = margin;
generateCoverXHTML() {
// get the image dimensions, and pass them to the coverSVG template
const { width, height } = sizeOf(this.coverImagePath)
const href = `images/${encodeURIComponent(this.coverEntry)}`
const svg = Xhtml.cover({ width, height, href })
// set the content string to be written once resolved
this.coverXHTMLContent = Template.render(svg, Xhtml.body())
log.info('cover build [cover.xhtml]')
}
request({url: imageUrl, encoding: null}, (err, r, data) => {
if (err) {
return res.status(500).send(`Unable to fetch ${imageUrl}`);
}
const contentType = r.headers['content-type'];
const imageHeight = Math.round(maxHeight / 2);
let imageWidth = 64;
if (tier.match(/sponsor/)) {
try {
const dimensions = sizeOf(data);
imageWidth = Math.round(dimensions.width / dimensions.height * imageHeight);
} catch (e) {
console.error("Unable to get image dimensions for ", imageUrl);
return res.status(500).send(`Unable to fetch ${imageUrl}`);
}
}
const base64data = new Buffer(data).toString('base64');
const svg = `<svg height="${imageHeight}" width="${imageWidth}" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg">
<image xlink:href="data:${contentType};base64,${base64data}" height="${imageHeight}" width="${imageWidth}"></image>
</svg>`;
res.setHeader('Cache-Control', 'public, max-age=300');
res.setHeader('content-type','image/svg+xml;charset=utf-8');
return res.send(svg);
});
} else {
makeImageDescriptionElement() {
let text
if (this.props.file !== null) {
const size = sizeOf(this.props.file.path)
const sizeDescription = size.width + 'x' + size.height
text = this.props.file.name + ' (' + sizeDescription + ')'
} else if (this.props.error !== null) {
text = this.props.error
} else {
text = 'Drag image onto the above box. Prefer 1024x1024 or larger'
}
const styles = {
div: {
display: 'flex',
justifyContent: 'center',
marginTop: '10px'
},
text: {
textAlign: 'center'
renderables = _.sortBy(images, [it => {
const {width, height} = sizeOf(it)
return (height / width)
}]).map((image, idx) => (
export async function putAttachment(imagePath, mimeType, category, processCallback, sendCallback, errorCallback) {
const { localPath, name } = processAttachment(imagePath, mimeType, category)
var mediaWidth = null
var mediaHeight = null
var thumbImage = null
if (category.endsWith('_IMAGE')) {
const dimensions = sizeOf(localPath)
mediaWidth = dimensions.width
mediaHeight = dimensions.height
thumbImage =
'iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAAA3NCSVQICAjb4U/gAAAAYUlEQVRoge3PQQ0AIBDAMMC/tBOFCB4Nyapg2zOzfnZ0wKsGtAa0BrQGtAa0BrQGtAa0BrQGtAa0BrQGtAa0BrQGtAa0BrQGtAa0BrQGtAa0BrQGtAa0BrQGtAa0BrQGtAub6QLkWqfRyQAAAABJRU5ErkJggg=='
}
var buffer = fs.readFileSync(localPath)
var key
var digest
const message = {
name: name,
mediaSize: buffer.byteLength,
mediaWidth: mediaWidth,
mediaHeight: mediaHeight,
mediaUrl: `file://${localPath}`,
mediaMimeType: mimeType,
thumbImage: thumbImage
if (!SUPPORTED_TYPES.includes(file.mime)) {
ctx.throw(400, `${file.filename} has invalid file type ${file.mime}.`)
}
const filenames = {
small: uuid() + '.jpg',
medium: uuid() + '.jpg',
large: uuid() + '.jpg',
original: uuid() + path.extname(file.filename)
}
fileMap.set(file.filename, filenames)
const fileOriginal = `${UPLOAD_PATH}/${filenames.original}`
await writeFile(file, fileOriginal)
const fileSize = sizeOf(fileOriginal)
if (fileSize.width < WIDTH_MINIMUM && fileSize.height < HEIGHT_MINIMUM) {
fileMap.forEach(filenames =>
fs.unlinkSync(`${UPLOAD_PATH}/${filenames.original}`)
)
ctx.throw(
400,
`File "${file.filename}" is ${fileSize.width}x${fileSize.height} pixels.
Valid screenshot must have width >= ${WIDTH_MINIMUM}px or height >= ${HEIGHT_MINIMUM}px.`
)
}
if (fileSize.width > WIDTH_MAXIMUM || fileSize.height > HEIGHT_MAXIMUM) {
fileMap.forEach(filenames =>
fs.unlinkSync(`${UPLOAD_PATH}/${filenames.original}`)
)