@yoooloo42/beat 1.0.20 → 1.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/libs/Base64.js +121 -0
- package/src/libs/Base64.js.test.png +0 -0
package/package.json
CHANGED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import https from 'https';
|
|
4
|
+
import http from 'http'; // 也可能需要处理 http 链接
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 从本地文件路径获取文件的 Base64 编码。
|
|
8
|
+
* @param {string} filePath - 本地文件的绝对或相对路径。
|
|
9
|
+
* @returns {Promise<string>} - 文件的 Base64 编码字符串。
|
|
10
|
+
*/
|
|
11
|
+
function getBase64FromLocalFile(filePath) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
// 确保路径是绝对路径
|
|
14
|
+
const absolutePath = path.resolve(filePath);
|
|
15
|
+
|
|
16
|
+
// 使用 { encoding: 'base64' } 直接读取为 Base64 字符串
|
|
17
|
+
fs.readFile(absolutePath, { encoding: 'base64' }, (err, data) => {
|
|
18
|
+
if (err) {
|
|
19
|
+
console.error(`[本地文件错误] 读取本地文件失败: ${err.message}`);
|
|
20
|
+
return reject(err);
|
|
21
|
+
}
|
|
22
|
+
resolve(data);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 从远程文件 URL 获取文件的 Base64 编码,支持自动重定向跟随 (3xx 状态码)。
|
|
29
|
+
* @param {string} fileUrl - 远程文件的完整 URL。
|
|
30
|
+
* @param {number} [redirects=5] - 最大重定向次数限制。
|
|
31
|
+
* @returns {Promise<string>} - 文件的 Base64 编码字符串。
|
|
32
|
+
*/
|
|
33
|
+
function getBase64FromURL(fileUrl, redirects = 5) {
|
|
34
|
+
if (redirects === 0) {
|
|
35
|
+
return Promise.reject(new Error(`[远程文件错误] 重定向次数过多 (${fileUrl})`));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 根据 URL 协议选择合适的模块
|
|
39
|
+
const client = fileUrl.startsWith('https') ? https : http;
|
|
40
|
+
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
client.get(fileUrl, (res) => {
|
|
43
|
+
const statusCode = res.statusCode;
|
|
44
|
+
|
|
45
|
+
// --- 处理重定向 (3xx 状态码) ---
|
|
46
|
+
if (statusCode >= 300 && statusCode < 400 && res.headers.location) {
|
|
47
|
+
const newUrl = res.headers.location;
|
|
48
|
+
console.log(`[重定向] 遇到状态码 ${statusCode},重定向至: ${newUrl}`);
|
|
49
|
+
// 递归调用自身,跟随重定向
|
|
50
|
+
return getBase64FromURL(newUrl, redirects - 1)
|
|
51
|
+
.then(resolve)
|
|
52
|
+
.catch(reject);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// --- 处理请求失败 (4xx, 5xx) ---
|
|
56
|
+
if (statusCode < 200 || statusCode >= 400) {
|
|
57
|
+
return reject(new Error(`[远程文件错误] 请求失败,状态码: ${statusCode} (${fileUrl})`));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// --- 成功获取 (2xx) ---
|
|
61
|
+
const chunks = [];
|
|
62
|
+
res.on('data', (chunk) => {
|
|
63
|
+
chunks.push(chunk);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
res.on('end', () => {
|
|
67
|
+
try {
|
|
68
|
+
// 合并所有 Buffer 块并转换为 Base64 字符串
|
|
69
|
+
const buffer = Buffer.concat(chunks);
|
|
70
|
+
const base64String = buffer.toString('base64');
|
|
71
|
+
resolve(base64String);
|
|
72
|
+
} catch (e) {
|
|
73
|
+
reject(new Error(`[远程文件错误] 处理远程文件数据失败: ${e.message}`));
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
}).on('error', (err) => {
|
|
78
|
+
console.error(`[远程文件错误] 远程请求连接错误: ${err.message}`);
|
|
79
|
+
reject(err);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/*
|
|
85
|
+
// ------------------------------------
|
|
86
|
+
// 示例执行
|
|
87
|
+
// ------------------------------------
|
|
88
|
+
|
|
89
|
+
// 1. 本地文件示例(请确保 'test.bmp' 存在于同一目录下)
|
|
90
|
+
// 注意:如果文件不存在,会捕获到错误。
|
|
91
|
+
getBase64FromLocalFile('Base64.js.test.png')
|
|
92
|
+
.then(base64String => {
|
|
93
|
+
console.log('\n--- 本地文件结果 ---');
|
|
94
|
+
console.log(`本地文件的 Base64 编码长度: ${base64String.length}`);
|
|
95
|
+
console.log(`本地文件的 Base64 编码 (前50字符): ${base64String.substring(0, 50)}...`);
|
|
96
|
+
})
|
|
97
|
+
.catch(error => {
|
|
98
|
+
// 捕获本地文件不存在或权限不足的错误
|
|
99
|
+
console.error('\n--- 本地文件错误 ---');
|
|
100
|
+
console.error('获取本地文件 Base64 失败:', error.message);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// 2. 远程 URL 示例(使用支持重定向的函数)
|
|
104
|
+
const remoteUrl = 'https://picsum.photos/200/300'; // 这个 URL 会导致 302 重定向
|
|
105
|
+
getBase64FromURL(remoteUrl)
|
|
106
|
+
.then(base64String => {
|
|
107
|
+
console.log('\n--- 远程文件结果 ---');
|
|
108
|
+
console.log(`远程文件的 Base64 编码长度: ${base64String.length}`);
|
|
109
|
+
console.log(`远程文件的 Base64 编码 (前50字符): ${base64String.substring(0, 50)}...`);
|
|
110
|
+
})
|
|
111
|
+
.catch(error => {
|
|
112
|
+
// 捕获请求失败或重定向超时的错误
|
|
113
|
+
console.error('\n--- 远程文件错误 ---');
|
|
114
|
+
console.error('获取远程文件 Base64 失败:', error.message);
|
|
115
|
+
});
|
|
116
|
+
*/
|
|
117
|
+
|
|
118
|
+
export default {
|
|
119
|
+
getBase64FromLocalFile,
|
|
120
|
+
getBase64FromURL
|
|
121
|
+
}
|
|
Binary file
|