我正在构建一个连接到 DMS (Alfresco) 的 Angular / Node.js 应用程序。服务器端 Node.js / Express 层充当代理,向客户端隐藏 Alfresco 的复杂性:
Angular client <--> Node backend <--> Alfresco
这个问题仅与 Node.js 后端有关。
上传文件时,我想将传入的文件直接转发到 Alfresco,而不是将其临时存储在磁盘上。使用临时磁盘存储可以按预期工作:
const fileUpload = require('express-fileupload');
const FormData = require('form-data');
// app is the express object
app.use(fileUpload({ createParentPath: true }));
app.post('/file', async (req, res) => {
// filedata is the FormData field of the client containing the actual file
let file = req.files.filedata;
let tmpPath = __dirname + '/tmp/' + file.name;
// save the file temporarily on the server
file.mv(tmpPath, async function(err) {
// create a new FormData Object for the upload to the DMS
const formData = new FormData();
formData.append('name', name);
// creates an fs.ReadStream object which is inherited from stream.Readable
formData.append('filedata', fs.createReadStream(tmpPath));
// upload the file to the DMS
let response = await axios.post('/files/...', formData, { headers: formData.getHeaders() });
// remove the temporary file
fs.rm(tmpPath, () => {
// return the answer of the DMS to the client
res.send(response.data);
});
});
});
现在我想避免磁盘访问并将文件直接转发到 DMS。考虑到在 Node.js 中将缓冲区转换为 ReadableStream,我尝试了以下三种替代方案。
const { Readable } = require('stream');
app.post('/file', async (req, res) => {
let file = req.files.fileData;
// create a new FormData Object for the upload to the DMS
const formData = new FormData();
formData.append('name', name);
/* alternatives starting here */
// Source: https://stackoverflow.com/questions/13230487/
// #1
const readable = new Readable();
readable._read = () => {};
readable.push(file.data);
readable.push(null);
// #2
const readable = new Readable();
readable._read = () => {};
const buffer = new Buffer(file.data, 'utf-8');
readable.push(buffer);
readable.push(null);
// #3
const readable = Readable.from(file.data);
/* alternatives ending here */
// put the Readable into the FormData object
formData.append('filedata', readable);
// upload the file to the DMS
let response = await axios.post('/files/...', formData, { headers: formData.getHeaders() });
// return the answer of the DMS to the client
res.send(response.data);
});
无论我尝试什么替代方案,Alfresco 总是抱怨,必填字段会丢失。尽管如此,仍提供了所有必填字段,因为临时存储文件的示例可以正常工作。我认为,Alfresco 无法处理我提供的流,并且我很难完全理解流在这种情况下是如何工作的。我应该怎么做?
请注意,为了便于阅读,所有错误处理以及 Alfresco 请求配置/API URL 都被省略了。