bun 多文件上传会有问题

This commit is contained in:
2025-05-11 21:57:54 +08:00
parent cdbebe92d0
commit 4aec2bc231
9 changed files with 510 additions and 691 deletions

View File

@@ -5,7 +5,7 @@
import { chalk } from '@/module/chalk.ts';
import { program, Command } from '../../../program.ts';
import { queryApp } from '../../../query/app-manager/query-app.ts';
import { checkAppDir, installApp, uninstallApp } from '@/module/download/install.ts';
import { checkAppDir, installApp, uninstallApp, fetchLink } from '@/module/download/install.ts';
import { fileIsExist } from '@/uitls/file.ts';
import fs from 'fs';
import { getConfig } from '@/module/get-config.ts';
@@ -156,3 +156,20 @@ const uninstallAppCommand = new Command('uninstall')
appCommand.addCommand(downloadAppCommand);
appCommand.addCommand(uninstallAppCommand);
const link = new Command('link')
.argument('url')
.option('-o --output <output>', '输出目录')
.action(async (url, options) => {
const output = options.output || '';
const { content, filename } = await fetchLink(url, { returnContent: true });
if (output) {
const checkOutput = fileIsExist(output);
if (!checkOutput) {
fs.mkdirSync(output, { recursive: true });
}
}
fs.writeFileSync(path.join(output, filename), content);
});
appCommand.addCommand(link);

View File

@@ -9,7 +9,9 @@ import inquirer from 'inquirer';
import { packLib, unpackLib } from './publish.ts';
import chalk from 'chalk';
import { installDeps } from '@/uitls/npm.ts';
import { MD5 } from 'crypto-js';
import cryptojs from 'crypto-js';
import { upload } from '@/module/download/upload.ts';
const MD5 = cryptojs.MD5;
/**
* 获取package.json 中的 basename, version, user, appKey
* @returns
@@ -209,8 +211,10 @@ const uploadFiles = async (files: string[], directory: string, opts: UploadFileO
console.log('文件已经上传过了', file);
continue;
}
const filename = path.basename(filePath);
console.log('upload file', file, filename);
form.append('file', fs.createReadStream(filePath), {
filename: file,
filename: filename,
filepath: file,
});
needUpload = true;
@@ -221,50 +225,12 @@ const uploadFiles = async (files: string[], directory: string, opts: UploadFileO
code: 200,
};
}
return new Promise(async (resolve) => {
const _baseURL = getBaseURL();
const url = new URL('/api/s1/resources/upload', _baseURL);
const searchParams = url.searchParams;
if (opts.noCheckAppFiles) {
searchParams.append('noCheckAppFiles', 'true');
}
console.log('upload url', url.hostname, url.protocol, url.port);
const pathname = url.href.toString().replace(_baseURL.toString(), '');
form.submit(
{
path: pathname,
host: url.hostname,
protocol: url.protocol as any,
port: url.port,
method: 'POST',
headers: {
Authorization: 'Bearer ' + token,
...form.getHeaders(),
},
},
(err, res) => {
if (err) {
console.error('Error uploading file:', err.message);
return;
}
// 处理服务器响应
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
try {
const res = JSON.parse(body);
resolve(res);
} catch (e) {
resolve({ code: 500, message: body });
}
});
},
);
});
const _baseURL = getBaseURL();
const url = new URL('/api/s1/resources/upload', _baseURL);
if (opts.noCheckAppFiles) {
url.searchParams.append('noCheckAppFiles', 'true');
}
return upload({ url: url, form: form, token: token });
};
app.addCommand(command);

View File

@@ -1,8 +1,11 @@
import { program as app, Command } from '@/program.ts';
const command = new Command('sync').description('同步项目').action(() => {
console.log('同步项目');
});
const command = new Command('sync')
.option('-d --dir <dir>')
.description('同步项目')
.action(() => {
console.log('同步项目');
});
const syncUpload = new Command('upload').description('上传项目').action(() => {
console.log('上传项目');
});