tools_batch_files 1.0.33 → 1.0.34

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tools_batch_files",
3
- "version": "1.0.33",
3
+ "version": "1.0.34",
4
4
  "description": "批处理视频工具",
5
5
  "keywords": [
6
6
  "utils",
@@ -13,6 +13,7 @@ const {
13
13
  ensureDirSync,
14
14
  isExist,
15
15
  readTxt,
16
+ removeDirectory,
16
17
  } = require("../../utils/settleFiles");
17
18
  const ffmpeg = require("fluent-ffmpeg");
18
19
  const fs = require("fs");
@@ -32,8 +33,12 @@ let taskIndex = 0;
32
33
  const exeDir = __dirname;
33
34
  //当前工作目录
34
35
  const workDir = process.cwd();
35
- // 表格目录
36
- const excelDir = path.join(workDir, "excel", "excel.xlsx");
36
+
37
+ // 表格目录,从excel文件夹中查找
38
+ const excelFiles = fs
39
+ .readdirSync(path.join(workDir, "excel"))
40
+ .find((file) => file.endsWith(".xlsx"));
41
+ const excelDir = path.join(workDir, "excel", excelFiles);
37
42
 
38
43
  const ORIGIN_FILE_DIR = "audio";
39
44
  const WM_AUDIO_DIR = "watermark_audio";
@@ -201,7 +206,7 @@ const archiveZip = (fileName, inputPath, originFilePath) => {
201
206
  */
202
207
  const getMetadata = async (
203
208
  originFilePath,
204
- { rowFileName, title, keyword, index }
209
+ { rowFileName, title, keyword, anotherId }
205
210
  ) => {
206
211
  //源音频数据
207
212
  const photoMetadataComand = ffmpeg(originFilePath);
@@ -219,7 +224,7 @@ const getMetadata = async (
219
224
  duration: "",
220
225
  tag_ids: "3",
221
226
  source_from: "sound_1",
222
- anotherId: rowFileName + index,
227
+ anotherId,
223
228
  };
224
229
 
225
230
  await new Promise((resolve, reject) => {
@@ -247,7 +252,7 @@ const getMetadata = async (
247
252
  /**
248
253
  * 接口
249
254
  */
250
- const postData = (dataParams, indexFilePath) => {
255
+ const postData = (dataParams, indexFilePath, index) => {
251
256
  const formData = new FormData();
252
257
 
253
258
  const zipFiles = fs
@@ -264,7 +269,7 @@ const postData = (dataParams, indexFilePath) => {
264
269
  }
265
270
  }
266
271
 
267
- logger("等待接口返回结果……");
272
+ logger(`第${index}条等待接口返回结果……`);
268
273
 
269
274
  // return axios.post("http://192.168.102.61:9999/upload/sound", formData, {
270
275
  // return axios.post("http://127.0.0.1:9999/upload/sound", formData, {
@@ -290,18 +295,19 @@ async function postDataWithRetry(
290
295
 
291
296
  while (retryCount < maxRetries) {
292
297
  try {
293
- const resData = await postData(dataParams, indexFilePath);
298
+ const resData = await postData(dataParams, indexFilePath, index);
294
299
  if (resData.data.code === 200) {
295
300
  logger("请求成功!");
296
301
  logger(resData.data.code);
297
302
  // 文件名和索引值
298
303
  successLogger(hash, index, fileName);
299
- fs.rmdirSync(indexFilePath, { recursive: true });
304
+ removeDirectory(indexFilePath);
300
305
  return;
301
306
  } else if (resData.data.code === 300) {
302
307
  // 重复上传,不捕获此错误
308
+ logger(resData.data.code);
303
309
  logger(`第${index}条文件-${index}重复上传!`);
304
- fs.rmdirSync(indexFilePath, { recursive: true });
310
+ removeDirectory(indexFilePath);
305
311
  return;
306
312
  } else {
307
313
  logger(`请求失败,重试中... (${retryCount + 1}/${maxRetries})`);
@@ -328,6 +334,11 @@ const task = async (row, index, hash) => {
328
334
  const rowFileName = row.fileName;
329
335
  const title = row.title;
330
336
  const keyword = row.keyword;
337
+ /**
338
+ * 唯一标识符id,仅错误excel中有 anotherI d 列
339
+ */
340
+ const anotherId = row.anotherId ? row.anotherId : excelFiles + index;
341
+
331
342
  try {
332
343
  logger(
333
344
  "**************************" + row.fileName + "**************************"
@@ -370,7 +381,7 @@ const task = async (row, index, hash) => {
370
381
  rowFileName,
371
382
  title,
372
383
  keyword,
373
- index,
384
+ anotherId,
374
385
  });
375
386
  await wavAudio(indexFilePath, originFilePath, fileName);
376
387
  await watermarkAudio(
@@ -394,8 +405,8 @@ const task = async (row, index, hash) => {
394
405
  } else {
395
406
  logger("音频任务失败(最外层catch):" + error);
396
407
  }
397
- fs.rmdirSync(indexFilePath, { recursive: true });
398
- disposeError(hash, { fileName, keyword, title });
408
+ removeDirectory(indexFilePath);
409
+ disposeError(hash, { fileName, keyword, title, anotherId });
399
410
 
400
411
  //失败的时候,复制文件到错误文件夹中
401
412
  // const failPath = path.join(workDir, Error_Files_Dir, fileName);
@@ -412,7 +423,10 @@ const run = (index, data, hash) => {
412
423
 
413
424
  if (!row) {
414
425
  taskIndex = 0;
415
- logger("当前任务,最后一条已结束!");
426
+ logger(
427
+ index +
428
+ "=========================当前任务,最后一条已结束!========================="
429
+ );
416
430
 
417
431
  // 是否存在错误列表? 存在的话,继续遍历
418
432
  const errorExcelPath = path.join(workDir, `error${hash}.xlsx`);
package/utils/logger.js CHANGED
@@ -25,9 +25,9 @@ const successLogger = (hash, index, fileName) => {
25
25
  };
26
26
 
27
27
  //错误日志- xlsx
28
- const disposeError = (hash, { fileName, keyword, title }) => {
28
+ const disposeError = (hash, { fileName, keyword, title, anotherId }) => {
29
29
  // 构建 Excel 数据
30
- const data = [[fileName, keyword, title]]; // 不包含表头
30
+ const data = [[fileName, keyword, title, anotherId]]; // 不包含表头
31
31
 
32
32
  // 读取已存在的 Excel 文件
33
33
  let existingData = [];
@@ -41,7 +41,7 @@ const disposeError = (hash, { fileName, keyword, title }) => {
41
41
  } catch (error) {
42
42
  logger("No existing file found, creating new one.");
43
43
  // 如果文件不存在,添加表头
44
- existingData.push(["fileName", "keyword", "title"]);
44
+ existingData.push(["fileName", "keyword", "title", "anotherId"]);
45
45
  }
46
46
 
47
47
  // 合并已有数据和新数据
@@ -49,7 +49,7 @@ function isExist(filePath) {
49
49
  console.log("存在");
50
50
  return true;
51
51
  } catch (err) {
52
- console.log("错了");
52
+ console.log("不存在");
53
53
  console.error(err);
54
54
  return false;
55
55
  }
@@ -95,6 +95,22 @@ function readTxt(path) {
95
95
  }
96
96
  }
97
97
 
98
+ /**
99
+ * 确保文件存在,再删除
100
+ */
101
+ function removeDirectory(directoryPath) {
102
+ try {
103
+ if (fs.existsSync(directoryPath)) {
104
+ fs.rmdirSync(directoryPath, { recursive: true });
105
+ console.log(`文件已删除: ${directoryPath}`);
106
+ } else {
107
+ console.log(`Directory does not exist: ${directoryPath}`);
108
+ }
109
+ } catch (error) {
110
+ console.error(`Error removing directory ${directoryPath}: ${error}`);
111
+ }
112
+ }
113
+
98
114
  module.exports = {
99
115
  findFileInDir,
100
116
  logFileSize,
@@ -102,4 +118,5 @@ module.exports = {
102
118
  moveFailFiles,
103
119
  isExist,
104
120
  readTxt,
121
+ removeDirectory,
105
122
  };