From 351fde268b27c843d7c5f91789944d75269cc43e Mon Sep 17 00:00:00 2001 From: wwei-github <418047394@qq.com> Date: Sat, 23 Dec 2023 09:58:35 +0800 Subject: [PATCH] feat: update betch upload --- index.js | 23 +++++++++++++++++++++-- src/logos/logos.controller.ts | 4 ++-- src/logos/logos.service.ts | 5 +---- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index f3ca2ed..ae2604b 100644 --- a/index.js +++ b/index.js @@ -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}`, @@ -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--; diff --git a/src/logos/logos.controller.ts b/src/logos/logos.controller.ts index d54ba5f..b537d24 100644 --- a/src/logos/logos.controller.ts +++ b/src/logos/logos.controller.ts @@ -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); } } diff --git a/src/logos/logos.service.ts b/src/logos/logos.service.ts index f96672d..5df1ac5 100644 --- a/src/logos/logos.service.ts +++ b/src/logos/logos.service.ts @@ -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 { @@ -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) => ({