|
|
|
|
|
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/'; |
|
|
|
|
|
|
|
|
const isLocalUpload = (url: string): boolean => url.startsWith('/uploads/', 0) && !IS_PROD |
|
|
|
|
|
export const hasLegacyPrefix = (url: string): boolean => url.startsWith(LEGACY_PREFIX, 0) |
|
|
|
|
|
const useProxy = (url: string): boolean => url.indexOf('spectrum.imgix.net') < 0 && url.startsWith('http', 0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 { |
|
|
|
|
|
if (useProxy(url)) return signProxy(processedUrl, opts); |
|
|
return signPrimary(processedUrl, opts); |
|
|
} catch (err) { |
|
|
|
|
|
console.error(err); |
|
|
return ''; |
|
|
} |
|
|
}; |
|
|
|