@blocklet/uploader-server 0.1.49 → 0.1.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,10 @@
1
1
  const { existsSync } = require("fs");
2
- const { join } = require("path");
2
+ const { join, basename } = require("path");
3
3
  const config = require("@blocklet/sdk/lib/config");
4
4
  const { getResources } = require("@blocklet/sdk/lib/component");
5
+ const httpProxy = require("http-proxy");
6
+ const joinUrl = require("url-join");
7
+ const proxy = httpProxy.createProxyServer();
5
8
  const logger = console;
6
9
  const ImgResourceType = "imgpack";
7
10
  const ImageBinDid = "z8ia1mAXo8ZE7ytGF36L5uBf9kD2kenhqFGp9";
@@ -93,15 +96,23 @@ export const initStaticResourceMiddleware = ({
93
96
  export const getCanUseResources = () => canUseResources;
94
97
  export const initProxyToMediaKitUploadsMiddleware = ({ options, express } = {}) => {
95
98
  return (req, res, next) => {
96
- if (!mediaKitInfo?.uploadsDir) {
99
+ if (!mediaKitInfo?.webEndpoint) {
97
100
  return next();
98
101
  }
99
- return express.static(mediaKitInfo.uploadsDir, {
100
- maxAge: "365d",
101
- immutable: true,
102
- index: false,
103
- // fallthrough: false,
104
- ...options
105
- })(req, res, next);
102
+ const filename = basename(req.url);
103
+ req.url = joinUrl("/uploads/", filename);
104
+ proxy.web(
105
+ req,
106
+ res,
107
+ {
108
+ target: mediaKitInfo.webEndpoint,
109
+ changeOrigin: true,
110
+ ...options
111
+ },
112
+ (err) => {
113
+ console.error("Proxy error:", err);
114
+ res.status(502).end();
115
+ }
116
+ );
106
117
  };
107
118
  };
@@ -8,12 +8,16 @@ const {
8
8
  existsSync
9
9
  } = require("fs");
10
10
  const {
11
- join
11
+ join,
12
+ basename
12
13
  } = require("path");
13
14
  const config = require("@blocklet/sdk/lib/config");
14
15
  const {
15
16
  getResources
16
17
  } = require("@blocklet/sdk/lib/component");
18
+ const httpProxy = require("http-proxy");
19
+ const joinUrl = require("url-join");
20
+ const proxy = httpProxy.createProxyServer();
17
21
  const logger = console;
18
22
  const ImgResourceType = "imgpack";
19
23
  const ImageBinDid = "z8ia1mAXo8ZE7ytGF36L5uBf9kD2kenhqFGp9";
@@ -118,16 +122,19 @@ const initProxyToMediaKitUploadsMiddleware = ({
118
122
  express
119
123
  } = {}) => {
120
124
  return (req, res, next) => {
121
- if (!mediaKitInfo?.uploadsDir) {
125
+ if (!mediaKitInfo?.webEndpoint) {
122
126
  return next();
123
127
  }
124
- return express.static(mediaKitInfo.uploadsDir, {
125
- maxAge: "365d",
126
- immutable: true,
127
- index: false,
128
- // fallthrough: false,
128
+ const filename = basename(req.url);
129
+ req.url = joinUrl("/uploads/", filename);
130
+ proxy.web(req, res, {
131
+ target: mediaKitInfo.webEndpoint,
132
+ changeOrigin: true,
129
133
  ...options
130
- })(req, res, next);
134
+ }, err => {
135
+ console.error("Proxy error:", err);
136
+ res.status(502).end();
137
+ });
131
138
  };
132
139
  };
133
140
  exports.initProxyToMediaKitUploadsMiddleware = initProxyToMediaKitUploadsMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/uploader-server",
3
- "version": "0.1.49",
3
+ "version": "0.1.51",
4
4
  "description": "blocklet upload server",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -46,12 +46,13 @@
46
46
  "@tus/server": "1.0.0",
47
47
  "@uppy/companion": "4.15.1",
48
48
  "body-parser": "^1.20.2",
49
+ "crypto": "^1.0.1",
49
50
  "express-session": "1.17.3",
51
+ "http-proxy": "^1.18.1",
50
52
  "mime-types": "^2.1.35",
51
53
  "p-queue": "^6.6.2",
52
54
  "ufo": "^1.5.3",
53
- "url-join": "^4.0.1",
54
- "crypto": "^1.0.1"
55
+ "url-join": "^4.0.1"
55
56
  },
56
57
  "devDependencies": {
57
58
  "@arcblock/eslint-config-ts": "^0.2.4",