This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,141 additions
and
317 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const router = require('express').Router() | ||
const { getGzipSharpStream } = require('./util') | ||
|
||
const V1_URL = `https://object.cscs.ch/v1` | ||
|
||
router.get('/v1', async (req, res) => { | ||
const { u } = req.query | ||
// deliberately do not do any path.join, to prevent path traversal | ||
const _url = `${V1_URL}${u}` | ||
|
||
res.setHeader('Content-Type', 'image/png') | ||
res.setHeader('Content-Encoding', 'gzip') | ||
const s = await getGzipSharpStream(_url) | ||
s.on('error', err => { | ||
res.status(500).send(err) | ||
}) | ||
s.pipe(res) | ||
}) | ||
|
||
|
||
module.exports = router |
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 tmp = require('tmp') | ||
const zlib = require('zlib') | ||
const { exec } = require('child_process') | ||
const { PassThrough, Readable } = require('stream') | ||
const sharp = require('sharp') | ||
const { APP_NAME } = require('../../constants') | ||
const fs = require('fs') | ||
const { store } = require('../../store') | ||
const MODULE_NAME = `[image/util]` | ||
|
||
const getStoreKey = key => `${APP_NAME}${MODULE_NAME}${key}` | ||
|
||
const { createGzip } = zlib | ||
|
||
const getBufferFromStore = async key => { | ||
const result = await store.get(key) | ||
if (result) { | ||
return Buffer.from(result, 'base64') | ||
} | ||
|
||
return null | ||
} | ||
|
||
const setBufferToStore = async (key, val) => { | ||
if (!(val instanceof Buffer)) { | ||
throw new Error(`val needs to be an instance of buffer!`) | ||
} else { | ||
await store.set(key, val.toString('base64')) | ||
} | ||
} | ||
|
||
const getGzipSharpStream = url => new Promise((rs, rj) => { | ||
tmp.file({ postfix: '.tif' }, async (err, filePath, fd, cleancb) => { | ||
if (err) { | ||
console.error(`[${APP_NAME}] generating temp file error`, err) | ||
res.status(500).send(err) | ||
return | ||
} | ||
const key = getStoreKey(url) | ||
const content = await getBufferFromStore(key) | ||
if (content) { | ||
const r = new Readable() | ||
rs(r) | ||
r.push(content) | ||
r.push(null) | ||
return | ||
} | ||
|
||
exec(`curl -o ${filePath} '${url}'`, (err, stdout, stderr) => { | ||
if (err) { | ||
console.error(err) | ||
return rj(err) | ||
} | ||
|
||
const gzip = createGzip() | ||
|
||
const passThrough = new PassThrough() | ||
const buf = [] | ||
passThrough.on('data', data => { | ||
buf.push(data) | ||
}) | ||
|
||
passThrough.on('end', () => { | ||
if (buf.length === 0) return | ||
const totalBuffer = Buffer.concat(buf) | ||
setBufferToStore(key, totalBuffer) | ||
}) | ||
|
||
const s = sharp(filePath) | ||
.png() | ||
.pipe(gzip) | ||
.pipe(passThrough) | ||
|
||
s.on('finish', () => { | ||
fs.unlink(filePath, () => cleancb()) | ||
}) | ||
|
||
rs(s) | ||
}) | ||
}) | ||
|
||
}) | ||
|
||
exports.setBufferToStore = setBufferToStore | ||
exports.getBufferFromStore = getBufferFromStore | ||
exports.getGzipSharpStream = getGzipSharpStream |
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.