|
1 | | -import { Buffer } from 'node:buffer'; |
| 1 | +import { open } from 'node:fs/promises'; |
2 | 2 |
|
3 | 3 | import type { App } from '@rocket.chat/apps-engine/definition/App'; |
4 | 4 | import { AppsEngineException } from '@rocket.chat/apps-engine/definition/exceptions/AppsEngineException'; |
5 | 5 | import type { IFileUploadContext } from '@rocket.chat/apps-engine/definition/uploads/IFileUploadContext' |
6 | 6 | import type { IUploadDetails } from '@rocket.chat/apps-engine/definition/uploads/IUploadDetails' |
7 | | -import { toArrayBuffer } from '@std/streams'; |
8 | 7 | import { Defined, JsonRpcError } from 'jsonrpc-lite'; |
9 | 8 |
|
10 | 9 | import { AppObjectRegistry } from '../../AppObjectRegistry'; |
@@ -42,25 +41,29 @@ export default async function handleUploadEvents(request: RequestContext): Promi |
42 | 41 | assertIsUpload(file); |
43 | 42 | assertString(path); |
44 | 43 |
|
45 | | - using tempFile = await Deno.open(path, { read: true, create: false }); |
46 | | - let context: IFileUploadContext; |
| 44 | + const tempFile = await open(path, 'r'); |
47 | 45 |
|
48 | | - switch (method) { |
49 | | - case 'executePreFileUpload': { |
50 | | - const fileContents = await toArrayBuffer(tempFile.readable); |
51 | | - context = { file, content: Buffer.from(fileContents) }; |
52 | | - break; |
| 46 | + try { |
| 47 | + let context: IFileUploadContext; |
| 48 | + |
| 49 | + switch (method) { |
| 50 | + case 'executePreFileUpload': { |
| 51 | + context = { file, content: await tempFile.readFile() }; |
| 52 | + break; |
| 53 | + } |
53 | 54 | } |
54 | | - } |
55 | 55 |
|
56 | | - return await handlerFunction.call( |
57 | | - wrapAppForRequest(app, request), |
58 | | - context, |
59 | | - AppAccessorsInstance.getReader(), |
60 | | - AppAccessorsInstance.getHttp(), |
61 | | - AppAccessorsInstance.getPersistence(), |
62 | | - AppAccessorsInstance.getModifier(), |
63 | | - ); |
| 56 | + return await handlerFunction.call( |
| 57 | + wrapAppForRequest(app, request), |
| 58 | + context, |
| 59 | + AppAccessorsInstance.getReader(), |
| 60 | + AppAccessorsInstance.getHttp(), |
| 61 | + AppAccessorsInstance.getPersistence(), |
| 62 | + AppAccessorsInstance.getModifier(), |
| 63 | + ); |
| 64 | + } finally { |
| 65 | + await tempFile.close(); |
| 66 | + } |
64 | 67 | } catch(e) { |
65 | 68 | if (e?.name === AppsEngineException.name) { |
66 | 69 | return new JsonRpcError(e.message, AppsEngineException.JSONRPC_ERROR_CODE, { name: e.name }); |
|
0 commit comments