File size: 2,391 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// @flow
require('now-env');
import ImgixClient from 'imgix-core-js';
import decodeUriComponent from 'decode-uri-component';
import { getDefaultExpires } from './getDefaultExpires';

const IS_PROD = process.env.NODE_ENV === 'production';
export const LEGACY_PREFIX = 'https://spectrum.imgix.net/';

// prettier-ignore
const isLocalUpload = (url: string): boolean => url.startsWith('/uploads/', 0) && !IS_PROD
// prettier-ignore
export const hasLegacyPrefix = (url: string): boolean => url.startsWith(LEGACY_PREFIX, 0)
// prettier-ignore
const useProxy = (url: string): boolean => url.indexOf('spectrum.imgix.net') < 0 && url.startsWith('http', 0)

/*
  When an image is uploaded to s3, we generate a url to be stored in our db
  that looks like: https://spectrum.imgix.net/users/:id/foo.png

  Because we are able to proxy our s3 bucket to imgix, we technically only
  needed to store the '/users/...' path. But since legacy threads and messages
  contain the full url, it must be stripped in order to generate a *new* signed
  url in this utility
*/
// prettier-ignore
export const stripLegacyPrefix = (url: string): string => url.replace(LEGACY_PREFIX, '')

type Opts = {
  expires: ?number,
};

const defaultOpts = {
  expires: getDefaultExpires(),
};

const signPrimary = (url: string, opts: Opts = defaultOpts): string => {
  const client = new ImgixClient({
    domains: 'spectrum.imgix.net',
    secureURLToken: process.env.IMGIX_SECURITY_KEY,
  });
  return client.buildURL(url, opts);
};

const signProxy = (url: string, opts?: Opts = defaultOpts): string => {
  const client = new ImgixClient({
    domains: 'spectrum-proxy.imgix.net',
    secureURLToken: process.env.IMGIX_PROXY_SECURITY_KEY,
  });
  return client.buildURL(url, opts);
};

export const signImageUrl = (url: string, opts: Opts = defaultOpts): string => {
  if (!url) return '';
  if (!opts.expires) {
    opts['expires'] = defaultOpts.expires;
  }

  if (isLocalUpload(url)) return url;

  const processedUrl = hasLegacyPrefix(url) ? stripLegacyPrefix(url) : url;

  try {
    // we never have to worry about escaping or unescaping proxied urls e.g. twitter images
    if (useProxy(url)) return signProxy(processedUrl, opts);
    return signPrimary(processedUrl, opts);
  } catch (err) {
    // if something fails, dont crash the entire frontend, just fail the images
    console.error(err);
    return '';
  }
};