Skip to content

Commit

Permalink
feat: add betch upload file
Browse files Browse the repository at this point in the history
  • Loading branch information
wwei-github committed Dec 23, 2023
1 parent a7060a6 commit 390fca3
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 1 deletion.
40 changes: 40 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions src/logos/dto/update-logo.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ export class CheckLogoDto {
required: true,
})
isAgree: boolean;

@IsNotEmpty()
@ApiProperty({
name: 'logoType',
type: String,
example: 'NFTs',
required: true,
})
logoType: string;
}
5 changes: 5 additions & 0 deletions src/logos/logos.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
35 changes: 35 additions & 0 deletions src/logos/logos.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -251,6 +252,10 @@ export class LogosService {
where: {
status: 'checking',
},
take: 10,
orderBy: {
id: 'asc',
},
include: {
logoName: true,
},
Expand All @@ -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);
}
}
22 changes: 22 additions & 0 deletions src/utils/batchUpload.ts
Original file line number Diff line number Diff line change
@@ -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]);
};

0 comments on commit 390fca3

Please sign in to comment.