Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit f54283f

Browse files
authored
Merge pull request #4158 from withspectrum/2.4.53-fix-3
2.4.53 fix 3
2 parents 90cccd2 + 1fde79f commit f54283f

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

api/apollo-server.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,28 @@ const server = new ProtectedApolloServer({
9898
return {
9999
user: user || null,
100100
loaders: createLoaders({ cache: false }),
101+
getImageSignatureExpiration: () => {
102+
const date = new Date();
103+
date.setHours(24);
104+
date.setMinutes(0);
105+
date.setSeconds(0);
106+
date.setMilliseconds(0);
107+
return date.getTime();
108+
},
101109
};
102110
})
103111
.catch(err => {
104112
console.error(err);
105113
return {
106114
loaders: createLoaders({ cache: false }),
115+
getImageSignatureExpiration: () => {
116+
const date = new Date();
117+
date.setHours(24);
118+
date.setMinutes(0);
119+
date.setSeconds(0);
120+
date.setMilliseconds(0);
121+
return date.getTime();
122+
},
107123
};
108124
}),
109125
},

api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"dataloader": "^1.4.0",
2525
"debounce": "^1.2.0",
2626
"debug": "^2.6.8",
27+
"decode-uri-component": "^0.2.0",
2728
"draft-js": "^0.10.5",
2829
"draft-js-code-editor-plugin": "0.2.1",
2930
"draft-js-drag-n-drop-plugin": "2.0.0-rc9",

api/utils/s3.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const uploadImage = async (
4141
const result = await file;
4242
const { filename, stream, mimetype } = result;
4343
const sanitized = sanitize(filename);
44+
const encoded = encodeURIComponent(sanitized);
4445
const validMediaTypes = ['image/gif', 'image/jpeg', 'image/png', 'video/mp4'];
4546
return new Promise(res => {
4647
// mimetype not in the validMediaType collection
@@ -53,7 +54,7 @@ export const uploadImage = async (
5354
}
5455

5556
const path = `spectrum-chat/${entity}/${id}`;
56-
const fileKey = `${shortid.generate()}-${sanitized}`;
57+
const fileKey = `${shortid.generate()}-${encoded}`;
5758
return s3.upload(
5859
{
5960
Bucket: path,
@@ -65,7 +66,7 @@ export const uploadImage = async (
6566
if (!data || !data.Key)
6667
throw new UserError('Image upload failed. Please try again.');
6768
const url = generateImageUrl(data.Key);
68-
res(encodeURI(url));
69+
res(url);
6970
}
7071
);
7172
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"dataloader": "^1.3.0",
7979
"debounce": "^1.2.0",
8080
"debug": "^2.6.8",
81+
"decode-uri-component": "^0.2.0",
8182
"draft-js": "npm:draft-js-fork-mxstbr",
8283
"draft-js-code-editor-plugin": "0.2.1",
8384
"draft-js-drag-n-drop-plugin": "^2.0.3",

shared/imgix/index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22
require('now-env');
33
import ImgixClient from 'imgix-core-js';
4+
import decodeUriComponent from 'decode-uri-component';
45

56
const IS_PROD = process.env.NODE_ENV === 'production';
67
const LEGACY_PREFIX = 'https://spectrum.imgix.net/';
@@ -12,6 +13,7 @@ const isLocalUpload = (url: string): boolean => url.startsWith('/uploads/', 0) &
1213
const hasLegacyPrefix = (url: string): boolean => url.startsWith(LEGACY_PREFIX, 0)
1314
// prettier-ignore
1415
const useProxy = (url: string): boolean => url.indexOf('spectrum.imgix.net') < 0 && url.startsWith('http', 0)
16+
const isEncoded = (url: string): boolean => url.indexOf('%') >= 0;
1517

1618
/*
1719
When an image is uploaded to s3, we generate a url to be stored in our db
@@ -50,8 +52,19 @@ export const signImageUrl = (url: string, opts: Opts) => {
5052

5153
if (isLocalUpload(url)) return url;
5254

53-
let processedUrl = hasLegacyPrefix(url) ? stripLegacyPrefix(url) : url;
55+
const processedUrl = hasLegacyPrefix(url) ? stripLegacyPrefix(url) : url;
5456

57+
// we never have to worry about escaping or unescaping proxied urls e.g. twitter images
5558
if (useProxy(url)) return signProxy(processedUrl, opts);
56-
return signPrimary(processedUrl, opts);
59+
60+
let decoded = processedUrl;
61+
if (isEncoded(processedUrl)) {
62+
const pathParts = decoded.split('/');
63+
const filename = pathParts.pop();
64+
const bucketPath = pathParts.join('/');
65+
decoded = bucketPath + '/' + encodeURIComponent(filename);
66+
decoded = decodeUriComponent(decoded);
67+
}
68+
69+
return signPrimary(decoded, opts);
5770
};

0 commit comments

Comments
 (0)