diff --git a/index.js b/index.js new file mode 100644 index 0000000..f3ca2ed --- /dev/null +++ b/index.js @@ -0,0 +1,40 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const axios = require('axios'); +const fs = require('fs'); +const path = require('path'); + +const filePath = path.join(__dirname, './svg'); +const files = fs.readdirSync(filePath).map((fileName) => ({ + path: `${filePath}/${fileName}`, + name: fileName.split('.')[0], + type: fileName.split('.')[1], +})); + +function chunkArray(array, chunkSize) { + const result = []; + for (let i = 0; i < array.length; i += chunkSize) { + result.push(array.slice(i, i + chunkSize)); + } + return result; +} +const filesArray = chunkArray(files, 10); + +let i = 0; +setInterval(async () => { + if (i >= filesArray.length) { + console.log('已经上传完毕'); + return; + } + try { + const result = await axios.post( + 'http://localhost:3000/logos/uploadImgByCode', + filesArray[i], + ); + console.log(result); + } catch (e) { + console.log(e); + i--; + } + i++; + console.log(i * 10); +}, 60000); diff --git a/package.json b/package.json index e1e7a23..6a4487d 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "@nestjs/platform-express": "^10.0.0", "@nestjs/swagger": "^7.1.13", "@prisma/client": "^5.4.2", - "axios": "^1.5.1", + "axios": "^1.6.2", "class-transformer": "^0.5.1", "class-validator": "^0.14.0", "reflect-metadata": "^0.1.13", @@ -39,6 +39,7 @@ "devDependencies": { "@commitlint/cli": "^17.7.2", "@commitlint/config-conventional": "^17.7.0", + "@lxdao/uploader3-connector": "^1.1.2", "@nestjs/cli": "^10.0.0", "@nestjs/schematics": "^10.0.0", "@nestjs/testing": "^10.0.0", diff --git a/src/logos/dto/update-logo.dto.ts b/src/logos/dto/update-logo.dto.ts index 9f41393..ec498c9 100644 --- a/src/logos/dto/update-logo.dto.ts +++ b/src/logos/dto/update-logo.dto.ts @@ -17,4 +17,13 @@ export class CheckLogoDto { required: true, }) isAgree: boolean; + + @IsNotEmpty() + @ApiProperty({ + name: 'logoType', + type: String, + example: 'NFTs', + required: true, + }) + logoType: string; } diff --git a/src/logos/logos.controller.ts b/src/logos/logos.controller.ts index c5f5bb2..d54ba5f 100644 --- a/src/logos/logos.controller.ts +++ b/src/logos/logos.controller.ts @@ -143,4 +143,9 @@ export class LogosController { async checkLogo(@Body() info: CheckLogoDto[]) { return await this.logosService.checkLogo(info); } + + @Post('/uploadImgByCode') + async uploadImgByCode(@Body() paths: any[]) { + return await this.logosService.uploadImgByCode(paths); + } } diff --git a/src/logos/logos.service.ts b/src/logos/logos.service.ts index 48408c5..f96672d 100644 --- a/src/logos/logos.service.ts +++ b/src/logos/logos.service.ts @@ -10,6 +10,7 @@ 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 { @@ -251,6 +252,10 @@ export class LogosService { where: { status: 'checking', }, + take: 10, + orderBy: { + id: 'asc', + }, include: { logoName: true, }, @@ -268,9 +273,39 @@ export class LogosService { }, data: { status: item.isAgree == true ? 'active' : 'reject', + logoName: { + update: { + logoType: item.logoType, + }, + }, + }, + include: { + logoName: true, }, }), ); return await this.prismaService.$transaction(updateTask); } + + async uploadImgByCode(path: any[]) { + const imgUrl = await batchUpload(path); + console.log(imgUrl); + const info = imgUrl.map( + (i) => + ({ + logoName: i.name, + logoType: '', + website: '', + files: [ + { + fileName: i.name, + fileType: i.type, + file: i.url, + }, + ], + authorAddress: '0x257c21206df8a751dE09B3502B32d25888099DB9', + } as unknown as CreateLogoDto), + ); + await this.batchUploadFile(info); + } } diff --git a/src/utils/batchUpload.ts b/src/utils/batchUpload.ts new file mode 100644 index 0000000..149a5d4 --- /dev/null +++ b/src/utils/batchUpload.ts @@ -0,0 +1,22 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const fs = require('fs'); +const path = require('path'); +const { createConnector } = require('@lxdao/uploader3-connector'); + +const connector = createConnector('NFT.storage', { + token: process.env.IPFS_TOKEN, +}); + +export default 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 } as { + name: string; + url: string; + type: string; + }; + }); + return Promise.all([...imgData]); +};