0.0.78 • Published 10 days ago

@blocklet/uploader v0.0.78

Weekly downloads
-
License
ISC
Repository
-
Last release
10 days ago

@blocklet/uploader

@blocklet/uploader is a package that integrates the uppy service to provide universal upload capability for blocklets. For more information about uppy, refer to the official documentation.

Package Structure

The package is composed of both frontend and backend components. The frontend code resides in the react folder, while the backend code can be found in the middlewares folder.

Development

Install In Blocklet

# You can use npm / yarn
pnpm add @blocklet/uploader

Install Dependencies

To install the required dependencies, run the following command:

pnpm i

Build Packages

To build the packages, execute the following command:

pnpm build

Build, Watch, and Run Development Server

For building, watching changes, and running the development server, use the following command:

pnpm run dev

Frontend Example

import { lazy } from 'react';

// eslint-disable-next-line import/no-unresolved
const UploaderComponent = lazy(() => import('@blocklet/uploader/react').then((res) => ({ default: res.Uploader })));

<UploaderComponent
  key="uploader"
  ref={uploaderRef}
  popup
  onUploadFinish={(result) => {
    prependUpload(result.data);
  }}
  plugins={['ImageEditor', 'Url', 'Webcam', canUseUnsplash && 'Unsplash'].filter(Boolean)}
  coreProps={{
    restrictions: {
      allowedFileTypes,
      maxFileSize,
    },
  }}
  apiPathProps={{
    uploader: '/api/uploads',
    companion: '/api/companion',
  }}
/>;

Backend Example

const { initLocalStorageServer, initCompanion } = require('@blocklet/uploader/middlewares');

// init uploader server
const localStorageServer = initLocalStorageServer({
  path: env.uploadDir,
  express,
  onUploadFinish: async (req, res, uploadMetadata) => {
    const {
      id: filename,
      size,
      metadata: { filename: originalname, filetype: mimetype },
    } = uploadMetadata;

    const obj = new URL(env.appUrl);
    obj.protocol = req.get('x-forwarded-proto') || req.protocol;
    obj.pathname = joinUrl(req.headers['x-path-prefix'] || '/', '/uploads', filename);

    const doc = await Upload.insert({
      mimetype,
      originalname,
      filename,
      size,
      remark: req.body.remark || '',
      tags: (req.body.tags || '')
        .split(',')
        .map((x) => x.trim())
        .filter(Boolean),
      folderId: req.componentDid,
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString(),
      createdBy: req.user.did,
      updatedBy: req.user.did,
    });

    const resData = { url: obj.href, ...doc };

    return resData;
  },
});

router.use('/uploads', user, auth, ensureComponentDid, localStorageServer.handle);

// if you need to load file from remote
// companion
const companion = initCompanion({
  path: env.uploadDir,
  express,
  providerOptions: env.providerOptions,
  uploadUrls: [env.appUrl],
});

router.use('/companion', user, auth, ensureComponentDid, companion.handle);

License

This package is licensed under the MIT license.

0.0.78

10 days ago

0.0.77

18 days ago

0.0.76

21 days ago

0.0.75

1 month ago

0.0.74

2 months ago

0.0.73

3 months ago

0.0.72

3 months ago

0.0.70

3 months ago

0.0.71

3 months ago

0.0.69

3 months ago

0.0.68

3 months ago

0.0.67

3 months ago

0.0.66

3 months ago

0.0.65

3 months ago

0.0.63

4 months ago

0.0.64

4 months ago

0.0.62

4 months ago

0.0.60

4 months ago

0.0.61

4 months ago

0.0.59

4 months ago

0.0.57

4 months ago

0.0.58

4 months ago

0.0.56

4 months ago

0.0.54

4 months ago

0.0.55

4 months ago

0.0.53

5 months ago

0.0.52

5 months ago

0.0.46

5 months ago

0.0.47

5 months ago

0.0.51

5 months ago

0.0.50

5 months ago

0.0.48

5 months ago

0.0.49

5 months ago

0.0.45

5 months ago

0.0.44

5 months ago

0.0.43

5 months ago

0.0.42

5 months ago

0.0.41

5 months ago

0.0.40

6 months ago

0.0.39

6 months ago

0.0.38

6 months ago

0.0.37

6 months ago

0.0.36

6 months ago

0.0.35

6 months ago

0.0.34

6 months ago

0.0.33

6 months ago

0.0.32

6 months ago

0.0.31

6 months ago

0.0.30

7 months ago

0.0.29

7 months ago

0.0.28

7 months ago

0.0.26

7 months ago

0.0.25

7 months ago

0.0.24

7 months ago

0.0.23

7 months ago

0.0.22

7 months ago

0.0.21

7 months ago

0.0.20

7 months ago

0.0.19

7 months ago

0.0.18

7 months ago

0.0.17

7 months ago

0.0.16

7 months ago

0.0.15

7 months ago

0.0.14

8 months ago

0.0.13

8 months ago

0.0.12

8 months ago

0.0.11

8 months ago

0.0.10

8 months ago

0.0.9

8 months ago

0.0.8

8 months ago

0.0.7

8 months ago

0.0.6

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago