opex792 commited on
Commit
eb7c18d
·
verified ·
1 Parent(s): 7dc09d2

Upload index.js

Browse files
Files changed (1) hide show
  1. index.js +22 -10
index.js CHANGED
@@ -2,7 +2,7 @@ import express from 'express';
2
  import multer from 'multer';
3
  import { spawn } from 'child_process';
4
  import { writeFile, unlink } from 'fs/promises';
5
- import { createReadStream } from 'fs'; // <<< ИСПРАВЛЕНИЕ: Правильный импорт
6
  import path from 'path';
7
  import { v4 as uuidv4 } from 'uuid';
8
  import fetch from 'node-fetch';
@@ -30,19 +30,19 @@ const downloadFile = async (url) => {
30
  };
31
 
32
 
33
- // --- СТАРЫЙ ЭНДПОИНТ ДЛЯ ПОТОКОВОЙ ОБРАБОТКИ (ВОЗВРАЩЕН) ---
34
  app.post('/api/run/stream', upload.single('file'), async (req, res) => {
35
  try {
36
  const { command, args: argsJson, file_url } = req.body;
37
  const file = req.file;
38
 
39
- if (!command) return res.status(400).send({ error: 'Parameter "command" is required.' });
40
 
41
  let args;
42
  try {
43
  args = argsJson ? JSON.parse(argsJson) : [];
44
  } catch(e) {
45
- return res.status(400).send({ error: 'Parameter "args" must be a valid JSON array.' });
46
  }
47
 
48
  let inputBuffer;
@@ -51,22 +51,35 @@ app.post('/api/run/stream', upload.single('file'), async (req, res) => {
51
  } else if (file_url) {
52
  inputBuffer = await downloadFile(file_url);
53
  } else {
54
- return res.status(400).send({ error: 'A file must be provided via "file" or "file_url".' });
55
  }
56
 
57
  const process = spawn(command, args);
 
58
  let stdoutChunks = [];
 
59
 
60
  process.stdout.on('data', (data) => stdoutChunks.push(data));
 
 
 
 
 
 
 
 
61
 
62
  process.on('close', (code) => {
63
  if (code === 0) {
64
  res.setHeader('Content-Type', 'application/octet-stream');
65
  res.send(Buffer.concat(stdoutChunks));
66
  } else {
67
- res.status(500).send({
 
 
68
  error: 'Command execution failed.',
69
- code: code
 
70
  });
71
  }
72
  });
@@ -75,7 +88,7 @@ app.post('/api/run/stream', upload.single('file'), async (req, res) => {
75
  process.stdin.end();
76
 
77
  } catch (error) {
78
- res.status(500).send({
79
  error: 'Server error during stream processing.',
80
  message: error.message
81
  });
@@ -83,7 +96,7 @@ app.post('/api/run/stream', upload.single('file'), async (req, res) => {
83
  });
84
 
85
 
86
- // --- НОВАЯ СИСТЕМА АСИНХРОННЫХ ЗАДАЧ ---
87
 
88
  const executeTask = async (taskId) => {
89
  const task = tasks[taskId];
@@ -266,7 +279,6 @@ app.get('/api/download/:fileId', (req, res) => {
266
 
267
  const filePath = path.join(TEMP_DIR, fileId);
268
 
269
- // ИСПРАВЛЕНИЕ: Используем createReadStream без промиса
270
  const stream = createReadStream(filePath);
271
  stream.on('error', (err) => {
272
  if (err.code === 'ENOENT') {
 
2
  import multer from 'multer';
3
  import { spawn } from 'child_process';
4
  import { writeFile, unlink } from 'fs/promises';
5
+ import { createReadStream } from 'fs';
6
  import path from 'path';
7
  import { v4 as uuidv4 } from 'uuid';
8
  import fetch from 'node-fetch';
 
30
  };
31
 
32
 
33
+ // --- ЭНДПОИНТ ДЛЯ ПОТОКОВОЙ ОБРАБОТКИ (ИСПРАВЛЕН) ---
34
  app.post('/api/run/stream', upload.single('file'), async (req, res) => {
35
  try {
36
  const { command, args: argsJson, file_url } = req.body;
37
  const file = req.file;
38
 
39
+ if (!command) return res.status(400).json({ error: 'Parameter "command" is required.' });
40
 
41
  let args;
42
  try {
43
  args = argsJson ? JSON.parse(argsJson) : [];
44
  } catch(e) {
45
+ return res.status(400).json({ error: 'Parameter "args" must be a valid JSON array.' });
46
  }
47
 
48
  let inputBuffer;
 
51
  } else if (file_url) {
52
  inputBuffer = await downloadFile(file_url);
53
  } else {
54
+ return res.status(400).json({ error: 'A file must be provided via "file" or "file_url".' });
55
  }
56
 
57
  const process = spawn(command, args);
58
+
59
  let stdoutChunks = [];
60
+ let stderrChunks = []; // <<< ДОБАВЛЕНО: Сбор ошибок
61
 
62
  process.stdout.on('data', (data) => stdoutChunks.push(data));
63
+ process.stderr.on('data', (data) => stderrChunks.push(data)); // <<< ДОБАВЛЕНО: Слушаем stderr
64
+
65
+ process.on('error', (err) => { // <<< ДОБАВЛЕНО: Обработка ошибок запуска
66
+ res.status(500).json({
67
+ error: 'Failed to start command.',
68
+ message: err.message
69
+ });
70
+ });
71
 
72
  process.on('close', (code) => {
73
  if (code === 0) {
74
  res.setHeader('Content-Type', 'application/octet-stream');
75
  res.send(Buffer.concat(stdoutChunks));
76
  } else {
77
+ // <<< ИЗМЕНЕНО: Отправляем подробную ошибку
78
+ const stderr = Buffer.concat(stderrChunks).toString('utf8');
79
+ res.status(500).json({
80
  error: 'Command execution failed.',
81
+ code: code,
82
+ stderr: stderr
83
  });
84
  }
85
  });
 
88
  process.stdin.end();
89
 
90
  } catch (error) {
91
+ res.status(500).json({
92
  error: 'Server error during stream processing.',
93
  message: error.message
94
  });
 
96
  });
97
 
98
 
99
+ // --- СИСТЕМА АСИНХРОННЫХ ЗАДАЧ ---
100
 
101
  const executeTask = async (taskId) => {
102
  const task = tasks[taskId];
 
279
 
280
  const filePath = path.join(TEMP_DIR, fileId);
281
 
 
282
  const stream = createReadStream(filePath);
283
  stream.on('error', (err) => {
284
  if (err.code === 'ENOENT') {