-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #263 from dfpc-coe/attachment-attach
Attachment Attach
- Loading branch information
Showing
13 changed files
with
830 additions
and
926 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import os from 'node:os'; | ||
import { DataPackage } from '@tak-ps/node-cot'; | ||
import { randomUUID } from 'node:crypto'; | ||
import type { Readable } from 'node:stream'; | ||
import { pipeline } from 'node:stream/promises'; | ||
import fs from 'node:fs'; | ||
import S3 from '../aws/s3.js'; | ||
import Config from '../config.js'; | ||
|
||
export default class AttachmentControl { | ||
config: Config; | ||
|
||
constructor(config: Config) { | ||
this.config = config; | ||
} | ||
|
||
async upload(name: string, body: Readable): Promise<{ | ||
hash: string | ||
}> { | ||
const tmp = os.tmpdir() + '/' + randomUUID(); | ||
|
||
try { | ||
fs.mkdirSync(tmp) | ||
await pipeline( | ||
body, | ||
fs.createWriteStream(tmp + '/' + name) | ||
); | ||
|
||
const hash = await DataPackage.hash(tmp + '/' + name); | ||
|
||
await S3.put( | ||
`attachment/${hash}/${name}`, | ||
fs.createReadStream(tmp + '/' + name) | ||
); | ||
|
||
return { hash }; | ||
} catch (err) { | ||
fs.unlinkSync(tmp + '/' + name); | ||
throw err; | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.