Skip to content

Commit

Permalink
Merge pull request #314 from blocklet/master
Browse files Browse the repository at this point in the history
chore: release to production store
  • Loading branch information
li-yechao authored Oct 30, 2024
2 parents 0315a46 + 5ee9cac commit 510d6b6
Show file tree
Hide file tree
Showing 39 changed files with 694 additions and 257 deletions.
16 changes: 16 additions & 0 deletions blocklets/image-bin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## 0.11.52 (October 30, 2024)

- chore: upgrade @blocklet/sdk

## 0.11.51 (October 30, 2024)

- chore: re-bump version

## 0.11.50 (October 30, 2024)

- chore: split @blocklet/uploader lib

## 0.11.49 (October 29, 2024)

- chore: update deps

## 0.11.48 (October 22, 2024)

- chore: update deps
Expand Down
47 changes: 46 additions & 1 deletion blocklets/image-bin/api/libs/auth.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const { SIG_VERSION } = require('@blocklet/constant');
const middleware = require('@blocklet/sdk/lib/middlewares');
const { verify } = require('@blocklet/sdk/lib/util/verify-sign');
const semVer = require('semver');
const { parseURL } = require('ufo');
const logger = require('./logger');

const auth = (req, res, next) => {
try {
const sig = req.get('x-component-sig');
if (sig) {
const verified = verify(req.body ?? {}, sig);
const verified = verifySig(req);
if (verified) {
next();
} else {
Expand All @@ -29,3 +32,45 @@ module.exports = {
user,
ensureAdmin,
};

const legacyFn = (req) => {
const data = req?.body ?? {};
const params = req?.query ?? {};
return { data, params };
};

const latestFn = (req) => {
const now = Math.floor(Date.now() / 1000);
const iat = Number(req.get('x-component-sig-iat'));
const exp = Number(req.get('x-component-sig-exp'));
if (Number.isNaN(iat) || Number.isNaN(exp)) {
throw new Error('invalid sig');
}
if (exp < now) {
throw new Error('expired sig');
}
const data = {
iat,
exp,
body: req.body ?? {},
query: req.query ?? {},
method: req.method.toLowerCase(),
url: parseURL(req.originalUrl).pathname,
};
return data;
};

const verifySig = (req) => {
const sig = req.get('x-component-sig');
const sigVersion = req.get('x-component-sig-version');
if (!sig) {
throw new Error('verify sig failed');
}
const getData = semVer.gt(semVer.coerce(sigVersion), semVer.coerce(SIG_VERSION.V0)) ? latestFn : legacyFn;
const data = getData(req);
const verified = verify(data, sig);
if (!verified) {
throw new Error('verify sig failed');
}
return true;
};
2 changes: 1 addition & 1 deletion blocklets/image-bin/api/routes/embed.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const express = require('express');
const { generateBlockletEmbed } = require('@blocklet/sdk/lib/embed');
const { generateBlockletEmbed } = require('@blocklet/embed');
const Upload = require('../states/upload');

const router = express.Router();
Expand Down
2 changes: 1 addition & 1 deletion blocklets/image-bin/api/routes/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path');
const express = require('express');
const toUpper = require('lodash/toUpper');
const flatten = require('lodash/flatten');
const { initStaticResourceMiddleware } = require('@blocklet/uploader/middlewares');
const { initStaticResourceMiddleware } = require('@blocklet/uploader-server');
const { getResourceExportDir, getResources } = require('@blocklet/sdk/lib/component');

const env = require('../libs/env');
Expand Down
2 changes: 1 addition & 1 deletion blocklets/image-bin/api/routes/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { LRUCache } = require('lru-cache');
const { isValid: isValidDID } = require('@arcblock/did');
const xbytes = require('xbytes');
const uniq = require('lodash/uniq');
const { initLocalStorageServer, initCompanion } = require('@blocklet/uploader/middlewares');
const { initLocalStorageServer, initCompanion } = require('@blocklet/uploader-server');
const logger = require('../libs/logger');
const { MEDIA_KIT_DID } = require('../libs/constants');
const { getResourceComponents } = require('./resources');
Expand Down
2 changes: 1 addition & 1 deletion blocklets/image-bin/blocklet.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: image-bin
version: 0.11.48
version: 0.11.52
title: Media Kit
description: A simple media assets upload and hosting blocklet
keywords:
Expand Down
13 changes: 8 additions & 5 deletions blocklets/image-bin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "image-bin",
"version": "0.11.48",
"version": "0.11.52",
"private": true,
"scripts": {
"dev": "blocklet dev",
Expand Down Expand Up @@ -40,12 +40,13 @@
"@arcblock/did": "^1.18.135",
"@arcblock/did-connect": "^2.10.48",
"@arcblock/ux": "^2.10.48",
"@blocklet/constant": "1.16.33-beta-20241001-015316-119b726d",
"@blocklet/constant": "1.16.33-beta-20241030-102817-90ad7cd5",
"@blocklet/embed": "^0.1.11",
"@blocklet/js-sdk": "1.16.33-beta-20241001-015316-119b726d",
"@blocklet/logger": "1.16.33-beta-20241001-015316-119b726d",
"@blocklet/sdk": "1.16.33-beta-20241001-015316-119b726d",
"@blocklet/js-sdk": "1.16.33-beta-20241030-102817-90ad7cd5",
"@blocklet/logger": "1.16.33-beta-20241030-102817-90ad7cd5",
"@blocklet/sdk": "1.16.33-beta-20241030-102817-90ad7cd5",
"@blocklet/uploader": "workspace:^",
"@blocklet/uploader-server": "workspace:^",
"@blocklet/xss": "workspace:^",
"@mui/icons-material": "^5.15.15",
"@mui/material": "^5.15.15",
Expand All @@ -61,7 +62,9 @@
"multer": "1.4.5-lts.1",
"nanoid": "^3.3.7",
"react-player": "^2.16.0",
"semver": "^7.6.3",
"serve-static": "^1.15.0",
"ufo": "^1.5.3",
"url-join": "^4.0.1",
"xbytes": "^1.9.1"
},
Expand Down
2 changes: 1 addition & 1 deletion blocklets/image-bin/src/components/history.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import CreateFolder from './create-folder';

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

const borderRadius = '4px !important';
Expand Down
15 changes: 13 additions & 2 deletions blocklets/image-bin/src/components/uploader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { useUploadContext } from '../contexts/upload';

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

const obj = new window.URL(window.location.origin);
Expand Down Expand Up @@ -77,6 +77,17 @@ function UploaderProviderWrapper({ children }) {
// target: document.body, // use body as drop target
}
}
// only for debug
// imageEditorProps={{
// cropperOptions: {
// aspectRatio: 16 / 9,
// },
// actions: {
// cropSquare: false,
// cropWidescreen: false,
// cropWidescreenVertical: false,
// },
// }}
apiPathProps={{
uploader: '/api/uploads',
companion: '/api/companion',
Expand Down
2 changes: 1 addition & 1 deletion blocklets/image-bin/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.11.48
0.11.52
2 changes: 1 addition & 1 deletion blocklets/image-bin/vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default defineConfig(({ mode }) => {
resolve: {
alias: [
{
find: '@blocklet/uploader/react',
find: '@blocklet/uploader',
replacement: join(__dirname, '../../packages/uploader/src/react.ts'),
},
{
Expand Down
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"zx": "^7.2.3"
},
"resolutions": {
"@abtnode/cron": "1.16.33-beta-20241001-015316-119b726d",
"@abtnode/cron": "1.16.33-beta-20241030-102817-90ad7cd5",
"@arcblock/did": "^1.18.135",
"@arcblock/did-auth": "^1.18.135",
"@arcblock/did-ext": "^1.18.135",
Expand All @@ -33,10 +33,10 @@
"@arcblock/validator": "^1.18.135",
"@arcblock/vc": "^1.18.135",
"@arcblock/ws": "^1.18.135",
"@blocklet/constant": "1.16.33-beta-20241001-015316-119b726d",
"@blocklet/js-sdk": "1.16.33-beta-20241001-015316-119b726d",
"@blocklet/logger": "1.16.33-beta-20241001-015316-119b726d",
"@blocklet/sdk": "1.16.33-beta-20241001-015316-119b726d",
"@blocklet/constant": "1.16.33-beta-20241030-102817-90ad7cd5",
"@blocklet/js-sdk": "1.16.33-beta-20241030-102817-90ad7cd5",
"@blocklet/logger": "1.16.33-beta-20241030-102817-90ad7cd5",
"@blocklet/sdk": "1.16.33-beta-20241030-102817-90ad7cd5",
"@mui/icons-material": "5.14.15",
"@mui/material": "5.14.15",
"@mui/system": "5.14.15",
Expand Down Expand Up @@ -71,5 +71,10 @@
"p-queue": "6.6.2",
"p-retry": "4.6.1",
"preact": "10.20.1"
},
"pnpm": {
"patchedDependencies": {
"@blocklet/sdk": "patches/@blocklet__sdk.patch"
}
}
}
9 changes: 9 additions & 0 deletions packages/uploader-server/.eslintrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import path from "path";

export default {
root: true,
extends: ["@arcblock/eslint-config-ts/base"],
parserOptions: {
project: path.resolve(__dirname, "tsconfig.eslint.json"),
},
};
9 changes: 9 additions & 0 deletions packages/uploader-server/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"printWidth": 120,
"useTabs": false,
"tabWidth": 2,
"trailingComma": "es5",
"bracketSameLine": true,
"semi": true,
"singleQuote": true
}
15 changes: 15 additions & 0 deletions packages/uploader-server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## 0.1.50 (October 30, 2024)

- chore: remove useless deps

## 0.1.49 (October 30, 2024)

- chore: re-bump version

## 0.1.48 (October 30, 2024)

- chore: split @blocklet/uploader lib

## 0.1.47 (October 29, 2024)

- feat: initial release
101 changes: 101 additions & 0 deletions packages/uploader-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# @blocklet/uploader-server-server

**@blocklet/uploader-server** 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](https://uppy.io/docs/quick-start/).

## Package Structure

The package is composed of both frontend and backend components. The backend code can be found in the `middlewares` folder.

## Development

### Install In Blocklet

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

### 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
```

## Backend Example

```javascript
const { initLocalStorageServer, initCompanion } = require('@blocklet/uploader-server');

// 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.
Loading

0 comments on commit 510d6b6

Please sign in to comment.