prettier formatted /cdn

This commit is contained in:
developomp 2021-10-20 09:14:48 +09:00
parent 99f6da9762
commit af73cb6993
6 changed files with 164 additions and 104 deletions

View File

@ -1,20 +1,26 @@
# Fosscord-CDN
CDN for Fosscord
## Run localy:
```
npm i
node dist/
```
## Endpoints:
### POST `/attachments/<filename>`
```
Content-Type: form-data
attachment: File (binary-data)
```
##### Returns:
```
{
"success": boolean, // true
@ -23,20 +29,28 @@ attachment: File (binary-data)
"filename": string // "lakdoiauej.png"
}
```
### GET `/attachments/<id>/<filename>`
```
requests image from database with given <id> and <filename>
```
##### Returns:
```
Content-Type: image/<imageType(png,img,gif)>
Image
```
### DELETE `/attachments/<id>/<filename>`
```
deletes database entry
```
##### Returns:
```
Content-Type: application/json
@ -49,6 +63,7 @@ Content-Type: application/json
<hr>
_(endpoints for crawler):_
### POST `/external`
```
@ -59,7 +74,9 @@ Content-Type: application/json
body:
{"url": URL} // "https://discord.com"
```
##### Returns:
```
Content-Type: application/json
@ -72,17 +89,23 @@ Content-Type: application/json
"ogType": string // "website"
}
```
### GET `/external/<id>/<filename>`
- requests cached crawled image
```
url-params:
:id // aHR0cHM6Ly9kaXNjb3JkLmNvbQ==
:filename // discord.png
```
```
/external/aHR0cHM6Ly9kaXNjb3JkLmNvbQ==/discord.png
```
##### Returns:
```
Content-Type: image/<imageType(png,img,gif)>
Image

View File

@ -5,30 +5,30 @@ import { Storage } from "./Storage";
const readableToBuffer = (readable: Readable): Promise<Buffer> =>
new Promise((resolve, reject) => {
const chunks: Buffer[] = [];
readable.on('data', chunk => chunks.push(chunk));
readable.on('error', reject);
readable.on('end', () => resolve(Buffer.concat(chunks)));
readable.on("data", (chunk) => chunks.push(chunk));
readable.on("error", reject);
readable.on("end", () => resolve(Buffer.concat(chunks)));
});
export class S3Storage implements Storage {
public constructor(
private client: S3,
private bucket: string,
private basePath?: string,
private basePath?: string
) {}
/**
* Always return a string, to ensure consistency.
*/
get bucketBasePath() {
return this.basePath ?? '';
return this.basePath ?? "";
}
async set(path: string, data: Buffer): Promise<void> {
await this.client.putObject({
Bucket: this.bucket,
Key: `${this.bucketBasePath}${path}`,
Body: data
Body: data,
});
}
@ -36,15 +36,15 @@ export class S3Storage implements Storage {
try {
const s3Object = await this.client.getObject({
Bucket: this.bucket,
Key: `${this.bucketBasePath ?? ''}${path}`
Key: `${this.bucketBasePath ?? ""}${path}`,
});
if (!s3Object.Body) return null;
const body = s3Object.Body;
return await readableToBuffer(<Readable> body);
} catch(err) {
return await readableToBuffer(<Readable>body);
} catch (err) {
console.error(`[CDN] Unable to get S3 object at path ${path}.`);
console.error(err);
return null;
@ -54,7 +54,7 @@ export class S3Storage implements Storage {
async delete(path: string): Promise<void> {
await this.client.deleteObject({
Bucket: this.bucket,
Key: `${this.bucketBasePath}${path}`
Key: `${this.bucketBasePath}${path}`,
});
}
}

View File

@ -2,7 +2,7 @@ import { FileStorage } from "./FileStorage";
import path from "path";
import fse from "fs-extra";
import { bgCyan, black } from "nanocolors";
import { S3 } from '@aws-sdk/client-s3';
import { S3 } from "@aws-sdk/client-s3";
import { S3Storage } from "./S3Storage";
process.cwd();
@ -27,17 +27,20 @@ if (process.env.STORAGE_PROVIDER === "file" || !process.env.STORAGE_PROVIDER) {
storage = new FileStorage();
} else if (process.env.STORAGE_PROVIDER === "s3") {
const
region = process.env.STORAGE_REGION,
const region = process.env.STORAGE_REGION,
bucket = process.env.STORAGE_BUCKET;
if (!region) {
console.error(`[CDN] You must provide a region when using the S3 storage provider.`);
console.error(
`[CDN] You must provide a region when using the S3 storage provider.`
);
process.exit(1);
}
if (!bucket) {
console.error(`[CDN] You must provide a bucket when using the S3 storage provider.`);
console.error(
`[CDN] You must provide a bucket when using the S3 storage provider.`
);
process.exit(1);
}
@ -45,7 +48,9 @@ if (process.env.STORAGE_PROVIDER === "file" || !process.env.STORAGE_PROVIDER) {
let location = process.env.STORAGE_LOCATION;
if (!location) {
console.warn(`[CDN] STORAGE_LOCATION unconfigured for S3 provider, defaulting to the bucket root...`);
console.warn(
`[CDN] STORAGE_LOCATION unconfigured for S3 provider, defaulting to the bucket root...`
);
location = undefined;
}

View File

@ -11,7 +11,12 @@ if (!process.env.STORAGE_PROVIDER) process.env.STORAGE_PROVIDER = "file";
if (process.env.STORAGE_PROVIDER === "file") {
if (process.env.STORAGE_LOCATION) {
if (!process.env.STORAGE_LOCATION.startsWith("/")) {
process.env.STORAGE_LOCATION = path.join(__dirname, "..", process.env.STORAGE_LOCATION, "/");
process.env.STORAGE_LOCATION = path.join(
__dirname,
"..",
process.env.STORAGE_LOCATION,
"/"
);
}
} else {
process.env.STORAGE_LOCATION = path.join(__dirname, "..", "files", "/");
@ -67,7 +72,9 @@ describe("/attachments", () => {
.set({ signature: Config.get().security.requestSignature })
.attach("file", __dirname + "/antman.jpg");
expect(response.statusCode).toBe(200);
expect(response.headers["content-type"]).toEqual(expect.stringContaining("json"));
expect(response.headers["content-type"]).toEqual(
expect.stringContaining("json")
);
expect(response.body.url).toBeDefined();
});
});
@ -79,7 +86,9 @@ describe("/attachments", () => {
.post("/attachments/123456789")
.set({ signature: Config.get().security.requestSignature })
.attach("file", __dirname + "/antman.jpg");
request.get(response.body.url.replace("http://localhost:3003", "")).then((x) => {
request
.get(response.body.url.replace("http://localhost:3003", ""))
.then((x) => {
expect(x.statusCode).toBe(200);
});
});
@ -92,7 +101,11 @@ describe("/attachments", () => {
.post("/attachments/123456789")
.set({ signature: Config.get().security.requestSignature })
.attach("file", __dirname + "/antman.jpg");
request.delete(response.body.url.replace("http://localhost:3003", "")).then((x) => {
request
.delete(
response.body.url.replace("http://localhost:3003", "")
)
.then((x) => {
expect(x.body.success).toBeDefined();
});
});
@ -123,7 +136,9 @@ describe("/avatars", () => {
.set({ signature: Config.get().security.requestSignature })
.attach("file", __dirname + "/antman.jpg");
expect(response.statusCode).toBe(200);
expect(response.headers["content-type"]).toEqual(expect.stringContaining("json"));
expect(response.headers["content-type"]).toEqual(
expect.stringContaining("json")
);
expect(response.body.url).toBeDefined();
});
});
@ -135,7 +150,9 @@ describe("/avatars", () => {
.post("/avatars/123456789")
.set({ signature: Config.get().security.requestSignature })
.attach("file", __dirname + "/antman.jpg");
request.get(response.body.url.replace("http://localhost:3003", "")).then((x) => {
request
.get(response.body.url.replace("http://localhost:3003", ""))
.then((x) => {
expect(x.statusCode).toBe(200);
});
});
@ -148,7 +165,11 @@ describe("/avatars", () => {
.post("/avatars/123456789")
.set({ signature: Config.get().security.requestSignature })
.attach("file", __dirname + "/antman.jpg");
request.delete(response.body.url.replace("http://localhost:3003", "")).then((x) => {
request
.delete(
response.body.url.replace("http://localhost:3003", "")
)
.then((x) => {
expect(x.body.success).toBeDefined();
});
});
@ -177,9 +198,13 @@ describe("/external", () => {
const response = await request
.post("/external")
.set({ signature: Config.get().security.requestSignature })
.send({ url: "https://i.ytimg.com/vi_webp/TiXzhQr5AUc/mqdefault.webp" });
.send({
url: "https://i.ytimg.com/vi_webp/TiXzhQr5AUc/mqdefault.webp",
});
expect(response.statusCode).toBe(200);
expect(response.headers["content-type"]).toEqual(expect.stringContaining("json"));
expect(response.headers["content-type"]).toEqual(
expect.stringContaining("json")
);
expect(response.body.id).toBeDefined();
});
});
@ -201,7 +226,9 @@ describe("/external", () => {
let response = await request
.post("/external")
.set({ signature: Config.get().security.requestSignature })
.send({ url: "https://i.ytimg.com/vi_webp/TiXzhQr5AUc/mqdefault.webp" });
.send({
url: "https://i.ytimg.com/vi_webp/TiXzhQr5AUc/mqdefault.webp",
});
request.get(`external/${response.body.id}`).then((x) => {
expect(x.statusCode).toBe(200);
});

View File

@ -7,7 +7,10 @@
"incremental": true /* Enable incremental compilation */,
"target": "ES6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"lib": ["ES2015", "dom"] /* Specify library files to be included in the compilation. */,
"lib": [
"ES2015",
"dom"
] /* Specify library files to be included in the compilation. */,
"allowJs": true /* Allow javascript files to be compiled. */,
"checkJs": true /* Report errors in .js files. */,
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
@ -48,7 +51,9 @@
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
"types": ["node"] /* Type declaration files to be included in compilation. */,
"types": [
"node"
] /* Type declaration files to be included in compilation. */,
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */