mirror of
https://github.com/kirameki-cafe/Yumi.git
synced 2026-09-13 10:49:20 +00:00
Create ImageProxy.ts
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { createHmac } from 'crypto';
|
||||
import Environment from '../providers/Environment';
|
||||
|
||||
export default class ImagePorxy {
|
||||
public static signImageProxyURL(url: string, modifiers: string | undefined = undefined) {
|
||||
const urlSafeBase64 = (string: Buffer) => {
|
||||
return Buffer.from(string).toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
|
||||
};
|
||||
|
||||
const hexDecode = (hex: string) => Buffer.from(hex, 'hex');
|
||||
|
||||
const sign = (salt: string, target: string, secret: string) => {
|
||||
const hmac = createHmac('sha256', hexDecode(secret));
|
||||
hmac.update(hexDecode(salt));
|
||||
hmac.update(target);
|
||||
return urlSafeBase64(hmac.digest());
|
||||
};
|
||||
|
||||
let path;
|
||||
if (modifiers) path = `/${modifiers}/plain/${url}`;
|
||||
else path = `/plain/${url}`;
|
||||
|
||||
const signature = sign(Environment.get().IMGPROXY_SALT, path, Environment.get().IMGPROXY_KEY);
|
||||
const result = `${Environment.get().IMGPROXY_HOST}${signature}${path}`;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user