Skip to content

Commit

Permalink
remove mime & fs
Browse files Browse the repository at this point in the history
  • Loading branch information
zcpua committed Jul 8, 2023
1 parent a939132 commit 1461f11
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 36 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
},
"homepage": "https://github.com/erictik/midjourney-client#readme",
"devDependencies": {
"@types/mime": "^3.0.1",
"@types/node": "^18.16.0",
"@types/ws": "^8.5.4",
"dotenv": "^16.0.3",
Expand All @@ -46,10 +45,9 @@
"dependencies": {
"@huggingface/inference": "^2.5.0",
"isomorphic-ws": "^5.0.0",
"mime": "^3.0.0",
"p-queue": "^6.6.2",
"snowyflake": "^2.0.0",
"tslib": "^2.5.0",
"ws": "^8.13.0"
}
}
}
73 changes: 51 additions & 22 deletions src/midjourne.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import {
} from "./interfaces";
import { CreateQueue } from "./queue";
import { nextNonce, sleep } from "./utls";
import * as fs from "fs";
import path from "path";
import * as mime from "mime";
import { Command } from "./command";
export class MidjourneyApi extends Command {
private apiQueue = CreateQueue(1);
Expand Down Expand Up @@ -307,27 +305,58 @@ export class MidjourneyApi extends Command {
}
/**
*
* @param fileUrl http or local file path
* @param fileUrl http file path
* @returns
*/
async UploadImage(fileUrl: string) {
let fileData;
let mimeType;
let filename;
let file_size;

if (fileUrl.startsWith("http")) {
const response = await this.config.fetch(fileUrl);
fileData = await response.arrayBuffer();
mimeType = response.headers.get("content-type");
filename = path.basename(fileUrl) || "image.png";
file_size = fileData.byteLength;
} else {
fileData = await fs.promises.readFile(fileUrl);
mimeType = mime.getType(fileUrl);
filename = path.basename(fileUrl);
file_size = (await fs.promises.stat(fileUrl)).size;
async UploadImageByUri(fileUrl: string) {
const response = await this.config.fetch(fileUrl);
const fileData = await response.arrayBuffer();
const mimeType = response.headers.get("content-type");
const filename = path.basename(fileUrl) || "image.png";
const file_size = fileData.byteLength;
if (!mimeType) {
throw new Error("Unknown mime type");
}
const { attachments } = await this.attachments({
filename,
file_size,
id: this.UpId++,
});
const UploadSlot = attachments[0];
await this.uploadImage(UploadSlot, fileData, mimeType);
const resp: DiscordImage = {
id: UploadSlot.id,
filename: path.basename(UploadSlot.upload_filename),
upload_filename: UploadSlot.upload_filename,
};
return resp;
}
async UploadImageByFile(file: File) {
const fileData = await file.arrayBuffer();
const mimeType = file.type;
const filename = file.name || "image.png";
const file_size = fileData.byteLength;
if (!mimeType) {
throw new Error("Unknown mime type");
}
const { attachments } = await this.attachments({
filename,
file_size,
id: this.UpId++,
});
const UploadSlot = attachments[0];
await this.uploadImage(UploadSlot, fileData, mimeType);
const resp: DiscordImage = {
id: UploadSlot.id,
filename: path.basename(UploadSlot.upload_filename),
upload_filename: UploadSlot.upload_filename,
};
return resp;
}
async UploadImageByBole(blob: Blob, filename = "image.png") {
const fileData = await blob.arrayBuffer();
const mimeType = blob.type;
const file_size = fileData.byteLength;
if (!mimeType) {
throw new Error("Unknown mime type");
}
Expand All @@ -338,12 +367,12 @@ export class MidjourneyApi extends Command {
});
const UploadSlot = attachments[0];
await this.uploadImage(UploadSlot, fileData, mimeType);
const response: DiscordImage = {
const resp: DiscordImage = {
id: UploadSlot.id,
filename: path.basename(UploadSlot.upload_filename),
upload_filename: UploadSlot.upload_filename,
};
return response;
return resp;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class Midjourney extends MidjourneyMessage {
async Describe(imgUri: string) {
const wsClient = await this.getWsClient();
const nonce = nextNonce();
const DcImage = await this.MJApi.UploadImage(imgUri);
const DcImage = await this.MJApi.UploadImageByUri(imgUri);
const httpStatus = await this.MJApi.DescribeApi(DcImage, nonce);
if (httpStatus !== 204) {
throw new Error(`DescribeApi failed with status ${httpStatus}`);
Expand Down
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,6 @@
resolved "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz"
integrity sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==

"@types/mime@^3.0.1":
version "3.0.1"
resolved "https://registry.npmjs.org/@types/mime/-/mime-3.0.1.tgz"
integrity sha512-Y4XFY5VJAuw0FgAqPNd6NNoV44jbq9Bz2L7Rh/J6jLTiHBSBJa9fxqQIvkIld4GsoDOcCbvzOUAbLPsSKKg+uA==

"@types/node@*", "@types/node@^18.16.0":
version "18.16.0"
resolved "https://registry.npmjs.org/@types/node/-/node-18.16.0.tgz"
Expand Down Expand Up @@ -291,11 +286,6 @@ make-error@^1.1.1:
resolved "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==

mime@^3.0.0:
version "3.0.0"
resolved "https://registry.npmjs.org/mime/-/mime-3.0.0.tgz"
integrity sha512-jSCU7/VB1loIWBZe14aEYHU/+1UMEHoaO7qxCOVJOw9GgH72VAWppxNcjU+x9a2k3GSIBXNKxXQFqRvvZ7vr3A==

p-finally@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz"
Expand Down

0 comments on commit 1461f11

Please sign in to comment.