Skip to content

Commit

Permalink
feat: update betch upload
Browse files Browse the repository at this point in the history
  • Loading branch information
wwei-github committed Dec 23, 2023
1 parent 390fca3 commit 351fde2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
23 changes: 21 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@ const axios = require('axios');
const fs = require('fs');
const path = require('path');

/* eslint-disable @typescript-eslint/no-var-requires */
const { createConnector } = require('@lxdao/uploader3-connector');

const connector = createConnector('NFT.storage', {
token:'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweGNBMDNlMDM3QjM5OWE3M2NkOTUyMUMyZGRFZjVjMjI3QjBkNTIyOUYiLCJpc3MiOiJuZnQtc3RvcmFnZSIsImlhdCI6MTY5NjkxODk2NjUwNSwibmFtZSI6IndlYjNsb2dvLXRlc3QifQ.Dm-oKxHkPhf_KmFxlVqXeyd5Ybom-EQXQ_P2oMWyLmc'
});

const batchUpload = async (files) => {
const imgData = files.map(async (i) => {
const fileData = fs.readFileSync(i.path);
const buffer = Buffer.from(fileData).toString('base64');
const result = await connector.postImage({ data: buffer, type: i.type });
return { ...i, url: result.url };
});
return Promise.all([...imgData]);
}


const filePath = path.join(__dirname, './svg');
const files = fs.readdirSync(filePath).map((fileName) => ({
path: `${filePath}/${fileName}`,
Expand All @@ -26,11 +44,12 @@ setInterval(async () => {
return;
}
try {
const imgurl = await batchUpload(filesArray[i]);
const result = await axios.post(
'http://localhost:3000/logos/uploadImgByCode',
filesArray[i],
imgurl,
);
console.log(result);
console.log(result.data);
} catch (e) {
console.log(e);
i--;
Expand Down
4 changes: 2 additions & 2 deletions src/logos/logos.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class LogosController {
}

@Post('/uploadImgByCode')
async uploadImgByCode(@Body() paths: any[]) {
return await this.logosService.uploadImgByCode(paths);
async uploadImgByCode(@Body() imgUrl: any[]) {
return await this.logosService.uploadImgByCode(imgUrl);
}
}
5 changes: 1 addition & 4 deletions src/logos/logos.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { PrismaService } from 'src/prisma/prisma.service';
import { FindLogoNameQuery, PageSize } from './dto/find-logo.dto';
import { Prisma } from '@prisma/client';
import { log } from 'console';
import batchUpload from 'src/utils/batchUpload';

@Injectable()
export class LogosService {
Expand Down Expand Up @@ -287,9 +286,7 @@ export class LogosService {
return await this.prismaService.$transaction(updateTask);
}

async uploadImgByCode(path: any[]) {
const imgUrl = await batchUpload(path);
console.log(imgUrl);
async uploadImgByCode(imgUrl: any[]) {
const info = imgUrl.map(
(i) =>
({
Expand Down

0 comments on commit 351fde2

Please sign in to comment.