Skip to content

Commit

Permalink
refactor: issue
Browse files Browse the repository at this point in the history
  • Loading branch information
phamphong9981 committed Sep 30, 2024
1 parent dedd5b3 commit 460d1ae
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/services/evm/erc721_media_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ const {
MAX_CONTENT_LENGTH_BYTE,
S3_GATEWAY,
} = Config;
const axiosClient = axios.create({
responseType: 'arraybuffer',
timeout: parseInt(REQUEST_IPFS_TIMEOUT, 10),
maxContentLength: parseInt(MAX_CONTENT_LENGTH_BYTE, 10),
maxBodyLength: parseInt(MAX_BODY_LENGTH_BYTE, 10),
});
export class Erc721MediaHandler {
// download image/animation from media_uri, then upload to S3
static async uploadMediaToS3(media_uri?: string) {
Expand Down Expand Up @@ -85,21 +91,20 @@ export class Erc721MediaHandler {
throw e;
}
} else {
// case media uri is http/https uri
const mediaBuffer = await Erc721MediaHandler.downloadAttachment(
Erc721MediaHandler.parseIPFSUri(media_uri)
);
let type: string | undefined = (
await FileType.fileTypeFromBuffer(mediaBuffer)
)?.mime;
if (type === 'application/xml') {
type = 'image/svg+xml';
}
return {
linkS3: media_uri,
contentType: type,
key: undefined,
};
// case media uri is http/https uri: just support image, video, audio, model
const type = await this.getContentType(media_uri);
const isContentTypeSupported =
type.startsWith('image') ||
type.startsWith('video') ||
type.startsWith('audio') ||
type.startsWith('model');
return isContentTypeSupported
? {
linkS3: media_uri,
contentType: type,
key: undefined,
}
: null;
}
}
return null;
Expand Down Expand Up @@ -221,12 +226,6 @@ export class Erc721MediaHandler {

// dowload image/animation from http/https url
static async downloadAttachment(url: string) {
const axiosClient = axios.create({
responseType: 'arraybuffer',
timeout: parseInt(REQUEST_IPFS_TIMEOUT, 10),
maxContentLength: parseInt(MAX_CONTENT_LENGTH_BYTE, 10),
maxBodyLength: parseInt(MAX_BODY_LENGTH_BYTE, 10),
});
const fromGithub = url.startsWith('https://github.com');
const formatedUrl = fromGithub ? `${url}?raw=true` : url;
return axiosClient.get(formatedUrl).then((response: any) => {
Expand All @@ -235,6 +234,13 @@ export class Erc721MediaHandler {
});
}

// head request to get ContentType
static async getContentType(url: string): Promise<string> {
const fromGithub = url.startsWith('https://github.com');
const formatedUrl = fromGithub ? `${url}?raw=true` : url;
return axiosClient.head(formatedUrl).then((response) => response.headers['content-type']);
}

/**
* @description check is ipfs format supported.
* If true, from media uri => check and parse that uri to unique filename (base on ipfs) that saved on AWS
Expand Down

0 comments on commit 460d1ae

Please sign in to comment.