Skip to content
This repository was archived by the owner on Nov 5, 2025. It is now read-only.

[NEW] Add a new upload API: createUpload#318

Merged
d-gubert merged 4 commits intoalphafrom
file-uploads
Sep 18, 2020
Merged

[NEW] Add a new upload API: createUpload#318
d-gubert merged 4 commits intoalphafrom
file-uploads

Conversation

@shiqimei
Copy link
Copy Markdown
Contributor

@shiqimei shiqimei commented Sep 18, 2020

What? ⛵

Introduced a new upload API createUpload under the accessor modify.getCreator().getUploadCreator().

Why? 🤔

Closes #32

Links 🌎

https://app.clickup.com/t/24zkzh

PS 👀

The PR on the RC side: RocketChat/Rocket.Chat#18955

@shiqimei shiqimei changed the title Add new upload API: createUpload Add a new upload API: createUpload Sep 18, 2020
@shiqimei shiqimei changed the title Add a new upload API: createUpload [NEW] Add a new upload API: createUpload Sep 18, 2020
@codecov
Copy link
Copy Markdown

codecov bot commented Sep 18, 2020

Codecov Report

Merging #318 into alpha will decrease coverage by 0.06%.
The diff coverage is 40.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##            alpha     #318      +/-   ##
==========================================
- Coverage   53.20%   53.13%   -0.07%     
==========================================
  Files          82       83       +1     
  Lines        2934     2949      +15     
  Branches      427      429       +2     
==========================================
+ Hits         1561     1567       +6     
- Misses       1373     1382       +9     
Impacted Files Coverage Δ
src/server/accessors/UploadCreator.ts 36.36% <36.36%> (ø)
src/server/accessors/ModifyCreator.ts 56.52% <50.00%> (-0.30%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1fc8d4c...6613e51. Read the comment docs.

@shiqimei
Copy link
Copy Markdown
Contributor Author

shiqimei commented Sep 18, 2020

import { Buffer } from 'buffer';

import { IAppAccessors, IHttp, ILogger, IModify, IPersistence, IRead } from '@rocket.chat/apps-engine/definition/accessors';
import { App } from '@rocket.chat/apps-engine/definition/App';
import { IMessage, IPostMessageSent } from '@rocket.chat/apps-engine/definition/messages';
import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata';

export class FileUploadApp extends App implements IPostMessageSent {
    constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
        super(info, logger, accessors);
    }
    public async executePostMessageSent(message: IMessage, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise<void> {
        try {
            const { text, room } = message;

            if (!text) {
                return;
            }

            // ! An error will be thrown if without extension name
            // await modify.getCreator().getUploadCreator().createUpload(room, 'example', Buffer.from(files.txt, 'base64'));

            // ! An error will be thrown if extension is not matched with detected MIME type
            // await modify.getCreator().getUploadCreator().createUpload(room, 'example.png', Buffer.from(files.pdf, 'base64'));

            // Upload a txt file (plain text)
            // const r1 = await modify.getCreator().getUploadCreator().createUpload(room, 'example.txt', Buffer.from(files.txt, 'base64'));
            // console.log(`${ r1.name }: ${ r1.url }`);

            // Upload a pdf file (binary)
            // const r2 = await createUpload(room, 'example.pdf', Buffer.from(files.pdf, 'base64'));
            // console.log(`${ r2.name }: ${ r2.url }`);

            // Pressure Testing
            const { name, complete, uploading, updatedAt, url } = await modify
                .getCreator()
                .getUploadCreator()
                .createUpload(room, `${ Math.random() }.txt`, Buffer.from((Math.random() * 10 ** 6).toFixed(0)));
            console.log({ name, complete, uploading, updatedAt, url });
        } catch (err) {
            console.error(err);
        }
    }
}

Zip: fileupload_0.0.1.zip

@shiqimei
Copy link
Copy Markdown
Contributor Author

Know issue:
Upload more than one file in a event handler (once) will cause a forbidden error thrown by the DB. (Not sure the reason yet ..)

image

@d-gubert d-gubert self-requested a review September 18, 2020 21:55
@d-gubert d-gubert merged commit 29c1fe8 into alpha Sep 18, 2020
@d-gubert d-gubert deleted the file-uploads branch September 18, 2020 21:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement the Files

2 participants