Skip to content

Commit

Permalink
fix(thumbnail)!: remove file-type dependency for contentType lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-online committed May 8, 2024
1 parent d6fbd4c commit 389e1dc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
"main": "dist/index.cjs",
"module": "dist/index.js",
"exports": {
"import": "./dist/mjs/index.js",
"require": "./dist/cjs/index.js"
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"types": "./dist/index.d.ts"
}
},
"scripts": {
"clean": "rimraf dist node_modules/.cache",
Expand All @@ -22,6 +25,7 @@
"husky": "husky install"
},
"devDependencies": {
"@biomejs/biome": "^1.7.3",
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"@favware/npm-deprecate": "^1.0.7",
Expand Down Expand Up @@ -62,8 +66,6 @@
},
"packageManager": "yarn@4.2.1",
"dependencies": {
"@biomejs/biome": "^1.7.3",
"axios": "^1.6.8",
"file-type": "^19.0.0"
"axios": "^1.6.8"
}
}
16 changes: 6 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import axios, {
type AxiosRequestConfig,
type AxiosRequestHeaders,
} from "axios";
import { fileTypeFromBuffer } from "file-type";
import { BunnyCdnStreamError } from "./error";
import { BunnyCdnStreamVideo } from "./structures/Video";
import { lowerObject } from "./utils";
Expand Down Expand Up @@ -384,11 +383,11 @@ export class BunnyCdnStream {
/**
* Set the thumbnail
*
* NOTE: The file type is automatically detected from the buffer, however if it fails, it will default to ``image/jpeg``
* NOTE: It is recommended to use a module like `file-type` to set the content-type of the thumbnail
* @returns A {@link BunnyCdnStream.SetThumbnailVideoResponse} instance.
* @param videoId The video ID
* @param thumbnail A buffer/stream/url of the thumbnail
* @param overrideContentType The content type to override and skip the automatic detection
* @param contentType The content type of the thumbnail, required for non-readstream inputs
* @example
* ```typescript
* await stream.setThumbnail("0273f24a-79d1-d0fe-97ca-b0e36bed31es", readFileSync("thumbnail.jpg"))
Expand All @@ -397,19 +396,16 @@ export class BunnyCdnStream {
public async setThumbnail(
videoId: string,
thumbnail: Buffer | ReadStream | string,
overrideContentType?: string,
contentType?: string,
) {
const options = this.getOptions();
const ct = overrideContentType ? { mime: overrideContentType } : undefined;

if (typeof thumbnail !== "string")
options.headers["Content-Type"] =
thumbnail instanceof ReadStream
contentType ||
(thumbnail instanceof ReadStream
? "application/octet-stream"
: (
ct ||
(await fileTypeFromBuffer(thumbnail)) || { mime: "image/jpg" }
).mime;
: "image/jpeg");

options.url += `/library/${this.options.videoLibrary}/videos/${videoId}/thumbnail`;
options.method = "POST";
Expand Down
8 changes: 1 addition & 7 deletions tests/Bunny.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,11 @@ describe("BunnyCdnStream", () => {

test("GIVEN library w/ encoded video THEN can set thumbnail as png", async () => {
const thumbnail = readFileSync(resolve(__dirname, "data", "bunny.png"));
const res = await stream.setThumbnail(videoGuid, thumbnail);
const res = await stream.setThumbnail(videoGuid, thumbnail, "image/png");
expect(res).toEqual({ success: true, message: "OK", statusCode: 200 });
});

test("GIVEN library w/ encoded video THEN can set thumbnail as jpg", async () => {
const thumbnail = readFileSync(resolve(__dirname, "data", "bunny.jpg"));
const res = await stream.setThumbnail(videoGuid, thumbnail);
expect(res).toEqual({ success: true, message: "OK", statusCode: 200 });
});

test("GIVEN library w/ encoded video AND overriden mime THEN can set thumbnail as jpg", async () => {
const thumbnail = readFileSync(resolve(__dirname, "data", "bunny.jpg"));
const res = await stream.setThumbnail(videoGuid, thumbnail, "image/jpg");
expect(res).toEqual({ success: true, message: "OK", statusCode: 200 });
Expand Down

0 comments on commit 389e1dc

Please sign in to comment.