-
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.
feat(plugin-media): added imgur and delete functionality
- Loading branch information
1 parent
14964e8
commit 4eca236
Showing
11 changed files
with
393 additions
and
130 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,66 @@ | ||
import { Origami } from '@origami/core'; | ||
import Color from 'color'; | ||
import sharp from 'sharp'; | ||
import { Readable } from 'stream'; | ||
|
||
export const parseImg = ( | ||
stream: Readable, | ||
req: Origami.Server.Request, | ||
res: Origami.Server.Response, | ||
next: Origami.Server.NextFunction | ||
) => { | ||
let s = stream; | ||
|
||
try { | ||
let _modify = sharp(); | ||
_modify.on('error', (err) => { | ||
next(err); | ||
return; | ||
}); | ||
|
||
|
||
let width; | ||
if (req.query.width) width = parseInt(req.query.width); | ||
|
||
let height; | ||
if (req.query.height) height = parseInt(req.query.height); | ||
|
||
let left; | ||
if (req.query.left) left = parseInt(req.query.left); | ||
|
||
let top; | ||
if (req.query.top) top = parseInt(req.query.top); | ||
|
||
let fit; | ||
if (req.query.fit) fit = req.query.fit; | ||
|
||
let position; | ||
if (req.query.position) position = req.query.position.replace(/-/gm, ' '); | ||
|
||
let background = 'black'; | ||
if (req.query.bg) background = Color(req.query.bg).hex(); | ||
|
||
// Crop image | ||
if (top !== undefined && left !== undefined && width !== undefined && height !== undefined) { | ||
_modify = _modify.extract({top, left, width, height}); | ||
|
||
// Resize image | ||
} else if (width || height) { | ||
const resize: sharp.ResizeOptions = {}; | ||
if (fit) resize.fit = fit; | ||
if (position) resize.position = position; | ||
if (background) resize.background = background; | ||
|
||
_modify = _modify.resize(width, height, resize); | ||
} | ||
|
||
|
||
s = s.pipe(_modify); | ||
|
||
res.locals.content.set(s); | ||
next(); | ||
|
||
} catch (e) { | ||
next(e); | ||
} | ||
}; |
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
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,131 @@ | ||
import { Origami } from '@origami/core'; | ||
// tslint:disable-next-line match-default-export-name | ||
import request from 'request-promise-native'; | ||
import { MediaPostReq, PluginOptionsImgur } from '..'; | ||
import { parseImg } from '../lib/parseImg'; | ||
|
||
|
||
export const initialize = async (options: PluginOptionsImgur) => {}; | ||
|
||
|
||
interface MediaResource { | ||
id: string; | ||
type: string; | ||
providerInfo: { | ||
id: string; | ||
deleteHash: string; | ||
}; | ||
} | ||
|
||
|
||
export const handlerCreate = ( | ||
options: PluginOptionsImgur | ||
// @ts-ignore | ||
): Origami.Server.RequestHandler => async (req: MediaPostReq, res, next) => { | ||
if (!req.files) { | ||
next(new Error('No files uploaded')); | ||
return; | ||
} | ||
|
||
const [, file] = Object.entries(req.files)[0]; | ||
|
||
// tslint:disable-next-line await-promise | ||
const r = await request({ | ||
uri: 'https://api.imgur.com/3/image', | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Client-ID ${options.clientID}` | ||
}, | ||
|
||
formData: { | ||
image: file.data, | ||
type: 'file' | ||
}, | ||
json: true | ||
}); | ||
|
||
const m = res.app.get('store').model('media'); | ||
|
||
try { | ||
const data = (await m.create({ | ||
name: file.name, | ||
type: file.mimetype, | ||
provider: 'imgur', | ||
author: req.jwt.data.userId, | ||
providerInfo: { | ||
id: r.data.id, | ||
deleteHash: r.data.deletehash | ||
} | ||
})) as { id: string }; | ||
|
||
res.locals.content.set(data); | ||
|
||
next(); | ||
|
||
} catch (e) { | ||
next(e); | ||
} | ||
}; | ||
|
||
|
||
export const handlerGet = ( | ||
options: PluginOptionsImgur | ||
): Origami.Server.RequestHandler => async (req, res, next) => { | ||
const m = res.app.get('store').model('media'); | ||
|
||
|
||
const file = (await m.find({ id: req.params.mediaId })) as MediaResource; | ||
if (!file) { | ||
res.locals.responseCode = 'resource.errors.notFound'; | ||
next(); | ||
return; | ||
} | ||
res.header('content-type', file.type); | ||
|
||
|
||
const fileID = file.providerInfo.id; | ||
const fileType = file.type.split('/').pop(); | ||
const stream = request(`https://imgur.com/${fileID}.${fileType}`); | ||
stream.catch((err) => { | ||
next(err); | ||
return; | ||
}); | ||
|
||
// @ts-ignore Is a stream | ||
parseImg(stream, req, res, next); | ||
}; | ||
|
||
|
||
/** | ||
* Delete a media resource from Imgur and mark as deleted in DB | ||
*/ | ||
export const handlerDelete = (options: PluginOptionsImgur): Origami.Server.RequestHandler => | ||
async (req, res, next) => { | ||
// Lookup file in database first | ||
const m = res.app.get('store').model('media') as Origami.Store.Model; | ||
|
||
const file = (await m.find({ id: req.params.mediaId })) as MediaResource; | ||
|
||
if (!file) { | ||
next(); | ||
return; | ||
} | ||
|
||
// tslint:disable-next-line await-promise | ||
const r = await request({ | ||
uri: `https://api.imgur.com/3/image/${file.providerInfo.deleteHash}`, | ||
method: 'DELETE', | ||
headers: { | ||
Authorization: `Client-ID ${options.clientID}` | ||
}, | ||
json: true | ||
}); | ||
|
||
try { | ||
await m.delete(file.id); | ||
res.locals.content.responseCode = 'resource.success.deleted'; | ||
next(); | ||
} catch (e) { | ||
next(e); | ||
} | ||
}; |
Oops, something went wrong.