@websolutespa/bom-mixer-image 1.0.5 → 1.0.6

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @websolutespa/bom-mixer-image
2
2
 
3
+ ## 1.0.6
4
+
5
+ ### Patch Changes
6
+
7
+ - Modified: imageHandler, re-route not found to origin
8
+
3
9
  ## 1.0.5
4
10
 
5
11
  ### Patch Changes
package/dist/index.js CHANGED
@@ -38,6 +38,7 @@ module.exports = __toCommonJS(src_exports);
38
38
  var fs = __toESM(require("fs"));
39
39
  var path = __toESM(require("path"));
40
40
  var import_sharp = __toESM(require("sharp"));
41
+ var import_bom_core = require("@websolutespa/bom-core");
41
42
 
42
43
  // src/api/api.handler.ts
43
44
  function apiHandler(handler) {
@@ -94,7 +95,7 @@ function imageHandler({ domains = [] }) {
94
95
  response.setHeader("Cache-Control", `public, max-age=${IMAGE_MAX_AGE}`);
95
96
  return response.send(cachedImage);
96
97
  }
97
- const originalImage = await getOriginalImage(url, domains);
98
+ const originalImage = await getOriginalImage(url, domains, request.headers);
98
99
  if (originalImage) {
99
100
  const resizedImage = (0, import_sharp.default)(originalImage).resize({
100
101
  width,
@@ -117,10 +118,10 @@ function imageHandler({ domains = [] }) {
117
118
  }
118
119
  });
119
120
  }
120
- function getOriginalImage(url, domains) {
121
- return new Promise((resolve2, reject) => {
121
+ function getOriginalImage(url, domains, headers, skipUrlValidity = false) {
122
+ return new Promise((resolve, reject) => {
122
123
  if (url.indexOf("http") === 0) {
123
- if (!checkUrlValidity(url, domains)) {
124
+ if (!skipUrlValidity && !checkUrlValidity(url, domains)) {
124
125
  console.error("imageHandler.getOriginalImage.error invalid url");
125
126
  return reject({ status: 401, message: "invalid url" });
126
127
  }
@@ -135,7 +136,7 @@ function getOriginalImage(url, domains) {
135
136
  return reject({ status: response.status, message: response.statusText });
136
137
  }
137
138
  response.arrayBuffer().then((buffer) => {
138
- return resolve2(Buffer.from(buffer));
139
+ return resolve(Buffer.from(buffer));
139
140
  }).catch((error) => {
140
141
  return reject(error);
141
142
  });
@@ -143,23 +144,28 @@ function getOriginalImage(url, domains) {
143
144
  return reject(error);
144
145
  });
145
146
  } else {
146
- const filePath = path.resolve(".", "public", url);
147
- fs.readFile(filePath, (error, data) => {
148
- if (error) {
149
- return reject(error);
150
- }
151
- return resolve2(data);
152
- });
147
+ const filePath = path.join(process.cwd(), "public", url);
148
+ if (fs.existsSync(filePath)) {
149
+ fs.readFile(filePath, (error, data) => {
150
+ if (error) {
151
+ return reject(error);
152
+ }
153
+ return resolve(data);
154
+ });
155
+ } else {
156
+ const origin = (0, import_bom_core.getOrigin)(headers);
157
+ return getOriginalImage(`${origin}${url}`, domains, headers, true);
158
+ }
153
159
  }
154
160
  });
155
161
  }
156
162
  function getCachedImage(pathname) {
157
- return new Promise((resolve2, reject) => {
163
+ return new Promise((resolve, reject) => {
158
164
  fs.readFile(pathname, (error, data) => {
159
165
  if (error) {
160
166
  return reject(error);
161
167
  }
162
- return resolve2(data);
168
+ return resolve(data);
163
169
  });
164
170
  });
165
171
  }
package/dist/index.mjs CHANGED
@@ -2,6 +2,7 @@
2
2
  import * as fs from "fs";
3
3
  import * as path from "path";
4
4
  import sharp from "sharp";
5
+ import { getOrigin } from "@websolutespa/bom-core";
5
6
 
6
7
  // src/api/api.handler.ts
7
8
  function apiHandler(handler) {
@@ -58,7 +59,7 @@ function imageHandler({ domains = [] }) {
58
59
  response.setHeader("Cache-Control", `public, max-age=${IMAGE_MAX_AGE}`);
59
60
  return response.send(cachedImage);
60
61
  }
61
- const originalImage = await getOriginalImage(url, domains);
62
+ const originalImage = await getOriginalImage(url, domains, request.headers);
62
63
  if (originalImage) {
63
64
  const resizedImage = sharp(originalImage).resize({
64
65
  width,
@@ -81,10 +82,10 @@ function imageHandler({ domains = [] }) {
81
82
  }
82
83
  });
83
84
  }
84
- function getOriginalImage(url, domains) {
85
- return new Promise((resolve2, reject) => {
85
+ function getOriginalImage(url, domains, headers, skipUrlValidity = false) {
86
+ return new Promise((resolve, reject) => {
86
87
  if (url.indexOf("http") === 0) {
87
- if (!checkUrlValidity(url, domains)) {
88
+ if (!skipUrlValidity && !checkUrlValidity(url, domains)) {
88
89
  console.error("imageHandler.getOriginalImage.error invalid url");
89
90
  return reject({ status: 401, message: "invalid url" });
90
91
  }
@@ -99,7 +100,7 @@ function getOriginalImage(url, domains) {
99
100
  return reject({ status: response.status, message: response.statusText });
100
101
  }
101
102
  response.arrayBuffer().then((buffer) => {
102
- return resolve2(Buffer.from(buffer));
103
+ return resolve(Buffer.from(buffer));
103
104
  }).catch((error) => {
104
105
  return reject(error);
105
106
  });
@@ -107,23 +108,28 @@ function getOriginalImage(url, domains) {
107
108
  return reject(error);
108
109
  });
109
110
  } else {
110
- const filePath = path.resolve(".", "public", url);
111
- fs.readFile(filePath, (error, data) => {
112
- if (error) {
113
- return reject(error);
114
- }
115
- return resolve2(data);
116
- });
111
+ const filePath = path.join(process.cwd(), "public", url);
112
+ if (fs.existsSync(filePath)) {
113
+ fs.readFile(filePath, (error, data) => {
114
+ if (error) {
115
+ return reject(error);
116
+ }
117
+ return resolve(data);
118
+ });
119
+ } else {
120
+ const origin = getOrigin(headers);
121
+ return getOriginalImage(`${origin}${url}`, domains, headers, true);
122
+ }
117
123
  }
118
124
  });
119
125
  }
120
126
  function getCachedImage(pathname) {
121
- return new Promise((resolve2, reject) => {
127
+ return new Promise((resolve, reject) => {
122
128
  fs.readFile(pathname, (error, data) => {
123
129
  if (error) {
124
130
  return reject(error);
125
131
  }
126
- return resolve2(data);
132
+ return resolve(data);
127
133
  });
128
134
  });
129
135
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@websolutespa/bom-mixer-image",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "Mixer Image module of the BOM Repository",
5
5
  "keywords": [
6
6
  "bom",
@@ -1,3 +1,4 @@
1
+ import { IncomingHttpHeaders } from 'http';
1
2
  // Importing the 'fs' module to work with the file system
2
3
  import * as fs from 'fs';
3
4
  // Importing the 'NextApiRequest' and 'NextApiResponse' from 'next' for handling API requests and responses
@@ -7,6 +8,7 @@ import * as path from 'path';
7
8
  // Importing 'sharp' for image processing
8
9
  import sharp from 'sharp';
9
10
  // Importing 'apiHandler' from '../api/api.handler'
11
+ import { getOrigin } from '@websolutespa/bom-core';
10
12
  import { apiHandler } from '../api/api.handler';
11
13
 
12
14
  const IMAGE_MAX_AGE = 604800;
@@ -58,7 +60,7 @@ export function imageHandler({ domains = [] }: { domains: string[] }) {
58
60
  return response.send(cachedImage);
59
61
  }
60
62
  // Reading the original image
61
- const originalImage = await getOriginalImage(url, domains);
63
+ const originalImage = await getOriginalImage(url, domains, request.headers);
62
64
  // Checking if the original image exists
63
65
  if (originalImage) {
64
66
  // Resizing the original image and converting it to WebP format
@@ -92,12 +94,15 @@ export function imageHandler({ domains = [] }: { domains: string[] }) {
92
94
  }
93
95
 
94
96
  // Defining the 'getOriginalImage' function
95
- function getOriginalImage(url: string, domains: string[]): Promise<Buffer> {
97
+ function getOriginalImage(url: string, domains: string[], headers: IncomingHttpHeaders, skipUrlValidity: boolean = false): Promise<Buffer> {
96
98
  // Returning a new promise
97
99
  return new Promise((resolve, reject) => {
98
100
  // Checking if 'url' starts with 'http'
99
101
  if (url.indexOf('http') === 0) {
100
- if (!checkUrlValidity(url, domains)) {
102
+ if (
103
+ !skipUrlValidity &&
104
+ !checkUrlValidity(url, domains)
105
+ ) {
101
106
  console.error('imageHandler.getOriginalImage.error invalid url');
102
107
  return reject({ status: 401, message: 'invalid url' });
103
108
  }
@@ -126,7 +131,7 @@ function getOriginalImage(url: string, domains: string[]): Promise<Buffer> {
126
131
  } else {
127
132
  // Reading the image from the file system
128
133
  // console.log('imageHandler.getOriginalImage.isDevelopment', isDevelopment);
129
- const filePath = path.resolve('.', 'public', url);
134
+ const filePath = path.join(process.cwd(), 'public', url);
130
135
  /*
131
136
  const { serverRuntimeConfig } = getConfig();
132
137
  const filePath = path.join(serverRuntimeConfig.PROJECT_ROOT, 'public', url);
@@ -137,6 +142,20 @@ function getOriginalImage(url: string, domains: string[]): Promise<Buffer> {
137
142
  path.join(process.cwd(), url);
138
143
  */
139
144
  // console.log('imageHandler.getOriginalImage.filePath', filePath);
145
+ // Check if the file exists
146
+ if (fs.existsSync(filePath)) {
147
+ fs.readFile(filePath, (error, data) => {
148
+ if (error) {
149
+ return reject(error);
150
+ }
151
+ // Resolving the promise with the image data
152
+ return resolve(data);
153
+ });
154
+ } else {
155
+ const origin = getOrigin(headers);
156
+ return getOriginalImage(`${origin}${url}`, domains, headers, true);
157
+ }
158
+ /*
140
159
  fs.readFile(filePath, (error, data) => {
141
160
  if (error) {
142
161
  return reject(error);
@@ -144,6 +163,7 @@ function getOriginalImage(url: string, domains: string[]): Promise<Buffer> {
144
163
  // Resolving the promise with the image data
145
164
  return resolve(data);
146
165
  });
166
+ */
147
167
  }
148
168
  });
149
169
  }