@websolutespa/bom-mixer-image 1.0.4 → 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,19 @@
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
+
9
+ ## 1.0.5
10
+
11
+ ### Patch Changes
12
+
13
+ - Modified: imageHandler
14
+ - Updated dependencies
15
+ - @websolutespa/bom-core@1.7.6
16
+
3
17
  ## 1.0.4
4
18
 
5
19
  ### Patch Changes
package/dist/index.js CHANGED
@@ -38,7 +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_config = __toESM(require("next/config"));
41
+ var import_bom_core = require("@websolutespa/bom-core");
42
42
 
43
43
  // src/api/api.handler.ts
44
44
  function apiHandler(handler) {
@@ -95,7 +95,7 @@ function imageHandler({ domains = [] }) {
95
95
  response.setHeader("Cache-Control", `public, max-age=${IMAGE_MAX_AGE}`);
96
96
  return response.send(cachedImage);
97
97
  }
98
- const originalImage = await getOriginalImage(url, domains);
98
+ const originalImage = await getOriginalImage(url, domains, request.headers);
99
99
  if (originalImage) {
100
100
  const resizedImage = (0, import_sharp.default)(originalImage).resize({
101
101
  width,
@@ -118,10 +118,10 @@ function imageHandler({ domains = [] }) {
118
118
  }
119
119
  });
120
120
  }
121
- function getOriginalImage(url, domains) {
121
+ function getOriginalImage(url, domains, headers, skipUrlValidity = false) {
122
122
  return new Promise((resolve, reject) => {
123
123
  if (url.indexOf("http") === 0) {
124
- if (!checkUrlValidity(url, domains)) {
124
+ if (!skipUrlValidity && !checkUrlValidity(url, domains)) {
125
125
  console.error("imageHandler.getOriginalImage.error invalid url");
126
126
  return reject({ status: 401, message: "invalid url" });
127
127
  }
@@ -144,14 +144,18 @@ function getOriginalImage(url, domains) {
144
144
  return reject(error);
145
145
  });
146
146
  } else {
147
- const { serverRuntimeConfig } = (0, import_config.default)();
148
- const filePath = path.join(serverRuntimeConfig.PROJECT_ROOT, "public", url);
149
- fs.readFile(filePath, (error, data) => {
150
- if (error) {
151
- return reject(error);
152
- }
153
- return resolve(data);
154
- });
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
+ }
155
159
  }
156
160
  });
157
161
  }
package/dist/index.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
  import * as fs from "fs";
3
3
  import * as path from "path";
4
4
  import sharp from "sharp";
5
- import getConfig from "next/config";
5
+ import { getOrigin } from "@websolutespa/bom-core";
6
6
 
7
7
  // src/api/api.handler.ts
8
8
  function apiHandler(handler) {
@@ -59,7 +59,7 @@ function imageHandler({ domains = [] }) {
59
59
  response.setHeader("Cache-Control", `public, max-age=${IMAGE_MAX_AGE}`);
60
60
  return response.send(cachedImage);
61
61
  }
62
- const originalImage = await getOriginalImage(url, domains);
62
+ const originalImage = await getOriginalImage(url, domains, request.headers);
63
63
  if (originalImage) {
64
64
  const resizedImage = sharp(originalImage).resize({
65
65
  width,
@@ -82,10 +82,10 @@ function imageHandler({ domains = [] }) {
82
82
  }
83
83
  });
84
84
  }
85
- function getOriginalImage(url, domains) {
85
+ function getOriginalImage(url, domains, headers, skipUrlValidity = false) {
86
86
  return new Promise((resolve, reject) => {
87
87
  if (url.indexOf("http") === 0) {
88
- if (!checkUrlValidity(url, domains)) {
88
+ if (!skipUrlValidity && !checkUrlValidity(url, domains)) {
89
89
  console.error("imageHandler.getOriginalImage.error invalid url");
90
90
  return reject({ status: 401, message: "invalid url" });
91
91
  }
@@ -108,14 +108,18 @@ function getOriginalImage(url, domains) {
108
108
  return reject(error);
109
109
  });
110
110
  } else {
111
- const { serverRuntimeConfig } = getConfig();
112
- const filePath = path.join(serverRuntimeConfig.PROJECT_ROOT, "public", url);
113
- fs.readFile(filePath, (error, data) => {
114
- if (error) {
115
- return reject(error);
116
- }
117
- return resolve(data);
118
- });
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
+ }
119
123
  }
120
124
  });
121
125
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@websolutespa/bom-mixer-image",
3
- "version": "1.0.4",
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,7 +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'
10
- import getConfig from 'next/config';
11
+ import { getOrigin } from '@websolutespa/bom-core';
11
12
  import { apiHandler } from '../api/api.handler';
12
13
 
13
14
  const IMAGE_MAX_AGE = 604800;
@@ -59,7 +60,7 @@ export function imageHandler({ domains = [] }: { domains: string[] }) {
59
60
  return response.send(cachedImage);
60
61
  }
61
62
  // Reading the original image
62
- const originalImage = await getOriginalImage(url, domains);
63
+ const originalImage = await getOriginalImage(url, domains, request.headers);
63
64
  // Checking if the original image exists
64
65
  if (originalImage) {
65
66
  // Resizing the original image and converting it to WebP format
@@ -93,12 +94,15 @@ export function imageHandler({ domains = [] }: { domains: string[] }) {
93
94
  }
94
95
 
95
96
  // Defining the 'getOriginalImage' function
96
- function getOriginalImage(url: string, domains: string[]): Promise<Buffer> {
97
+ function getOriginalImage(url: string, domains: string[], headers: IncomingHttpHeaders, skipUrlValidity: boolean = false): Promise<Buffer> {
97
98
  // Returning a new promise
98
99
  return new Promise((resolve, reject) => {
99
100
  // Checking if 'url' starts with 'http'
100
101
  if (url.indexOf('http') === 0) {
101
- if (!checkUrlValidity(url, domains)) {
102
+ if (
103
+ !skipUrlValidity &&
104
+ !checkUrlValidity(url, domains)
105
+ ) {
102
106
  console.error('imageHandler.getOriginalImage.error invalid url');
103
107
  return reject({ status: 401, message: 'invalid url' });
104
108
  }
@@ -126,15 +130,32 @@ function getOriginalImage(url: string, domains: string[]): Promise<Buffer> {
126
130
  });
127
131
  } else {
128
132
  // Reading the image from the file system
129
- const { serverRuntimeConfig } = getConfig();
130
133
  // console.log('imageHandler.getOriginalImage.isDevelopment', isDevelopment);
134
+ const filePath = path.join(process.cwd(), 'public', url);
135
+ /*
136
+ const { serverRuntimeConfig } = getConfig();
131
137
  const filePath = path.join(serverRuntimeConfig.PROJECT_ROOT, 'public', url);
138
+ */
132
139
  /*
133
140
  const filePath = isDevelopment ?
134
141
  path.join(process.cwd(), 'public', url) :
135
142
  path.join(process.cwd(), url);
136
143
  */
137
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
+ /*
138
159
  fs.readFile(filePath, (error, data) => {
139
160
  if (error) {
140
161
  return reject(error);
@@ -142,6 +163,7 @@ function getOriginalImage(url: string, domains: string[]): Promise<Buffer> {
142
163
  // Resolving the promise with the image data
143
164
  return resolve(data);
144
165
  });
166
+ */
145
167
  }
146
168
  });
147
169
  }