Skip to content

Commit

Permalink
Merge pull request #6805 from reactioncommerce/fix/sample-image-data-…
Browse files Browse the repository at this point in the history
…not-showing

fix: sample image data not showing
  • Loading branch information
brent-hoover authored Mar 28, 2023
2 parents 08bbe34 + 3c01340 commit ecb8ef6
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/curly-clocks-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@reactioncommerce/api-plugin-sample-data": minor
"@reactioncommerce/file-collections": minor
"@reactioncommerce/file-collections-sa-gridfs": minor
---

fix: sample image data not showing
5 changes: 3 additions & 2 deletions packages/api-plugin-sample-data/src/loaders/loadImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable no-await-in-loop */
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { Readable } from "stream";
import pkg from "@reactioncommerce/file-collections";

Expand Down Expand Up @@ -106,8 +107,8 @@ export default async function loadImages(context, shopId) {

const topProdIds = [];
const fileType = "image/jpeg";
const folderPath = "./custom-packages/api-plugin-sample-data/src/images/";

const currentDir = path.dirname(fileURLToPath(import.meta.url));
const folderPath = path.join(currentDir, "../images/");
let fileList = [];
try {
fileList = fs.readdirSync(folderPath);
Expand Down
2 changes: 1 addition & 1 deletion packages/file-collections-sa-gridfs/src/GridFSStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class GridFSStore extends StorageAdapter {
// Add range if this should be a partial read
if (typeof startPos === "number" && typeof endPos === "number") {
opts.start = startPos;
opts.end = endPos;
opts.end = endPos + 1;
}

debug("GridFSStore _getReadStream opts:", opts);
Expand Down
3 changes: 3 additions & 0 deletions packages/file-collections/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
"main": "./dist/node/index.js",
"scripts": {
"build": "rm -rf dist/** && babel src --out-dir dist --ignore \"**/*.test.js\"",
"test": "jest",
"test:watch": "jest --watch",
"test:file": "jest --no-cache --watch --coverage=false",
"prepublishOnly": "npm run build"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function requestRange(headers, fileSize) {
if (String(startByte) !== start) startByte = 0;
if ((String(endByte) !== end) || endByte === 0) endByte = fileSize - 1;

if (start >= end) {
if (startByte >= endByte || endByte >= fileSize) {
return {
errorCode: 416,
errorMessage: "Requested Range Not Satisfiable"
Expand All @@ -68,11 +68,11 @@ export default function requestRange(headers, fileSize) {

const partSize = (endByte - startByte) + 1;
return {
end,
end: endByte,
len: partSize,
partial: (partSize < fileSize),
size: fileSize,
start,
start: startByte,
unit
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import requestRange from "./requestRange.js";

test("should return default setting when range header is not present", () => {
const headers = {};
const fileSize = 100;
const result = requestRange(headers, fileSize);
expect(result).toEqual({
end: 99,
len: 100,
partial: false,
size: 100,
start: 0,
unit: "bytes"
});
});

test("should return correct range when range header is present", () => {
const headers = { range: "bytes=0-999" };
const fileSize = 1000;
const result = requestRange(headers, fileSize);
expect(result).toEqual({
end: 999,
len: 1000,
partial: false,
size: 1000,
start: 0,
unit: "bytes"
});
});

test("should return the correct range when the range header request first half part of the file", () => {
const headers = { range: "bytes=0-499" };
const fileSize = 1000;
const result = requestRange(headers, fileSize);
expect(result).toEqual({
end: 499,
len: 500,
partial: true,
size: 1000,
start: 0,
unit: "bytes"
});
});

test("should return error when range header is present but file size is not", () => {
const headers = { range: "bytes=0-10" };
const fileSize = null;
const result = requestRange(headers, fileSize);
expect(result).toEqual({
errorCode: 416,
errorMessage: "Requested Range Not Satisfiable (Unknown File Size)"
});
});

test("should return error when range header is present but invalid", () => {
const headers = { range: "bytes" };
const fileSize = 100;
const result = requestRange(headers, fileSize);
expect(result).toEqual({
errorCode: 416,
errorMessage: "Requested Range Unit Not Satisfiable"
});
});

test('should return error when range header is present but unit is not a "bytes"', () => {
const headers = { range: "k_bytes=0-10" };
const fileSize = 100;
const result = requestRange(headers, fileSize);
expect(result).toEqual({
errorCode: 416,
errorMessage: "Requested Range Unit Not Satisfiable"
});
});

test("should return error when range header is present but start is greater than end", () => {
const headers = { range: "bytes=10-9" };
const fileSize = 100;
const result = requestRange(headers, fileSize);
expect(result).toEqual({
errorCode: 416,
errorMessage: "Requested Range Not Satisfiable"
});
});

test("should return error when range header is present but end is greater than file size", () => {
const headers = { range: "bytes=0-1000" };
const fileSize = 100;
const result = requestRange(headers, fileSize);
expect(result).toEqual({
errorCode: 416,
errorMessage: "Requested Range Not Satisfiable"
});
});

0 comments on commit ecb8ef6

Please sign in to comment.