Skip to content

Commit

Permalink
fix ENAMETOOLONG
Browse files Browse the repository at this point in the history
  • Loading branch information
jj committed Apr 6, 2021
1 parent 636fe2c commit 3159f72
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/images.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import probe from 'probe-image-size'
import { getCache, setCache } from '@lib/cache'

import { createReadStream, createWriteStream, existsSync, mkdirSync } from 'fs'
import { createReadStream, createWriteStream, existsSync } from 'fs'
import { join } from 'path'
import { processEnv } from '@lib/processEnv'
import { promisify } from 'util'

import { sha1 } from 'crypto-hash'

const streamPipeline = promisify(require('stream').pipeline)

/**
Expand All @@ -27,6 +29,13 @@ const maxRetries = 50
const read_timeout = 3000 // ms
const response_timeout = 3000 // ms

const calcHash = async (input: ArrayBuffer | string) => await sha1(input)

const genCacheKey = async (url: string, noCache?: boolean) => {
if (noCache) return null
return await calcHash(url)
}

export interface Dimensions {
width: number
height: number
Expand All @@ -35,7 +44,7 @@ export interface Dimensions {
export const imageDimensions = async (url: string | undefined | null, noCache?: boolean): Promise<Dimensions | null> => {
if (!url) return null

const cacheKey = (!noCache && encodeURIComponent(url)) || null
const cacheKey = await genCacheKey(url, noCache)
const cached = getCache<Dimensions>(cacheKey)
if (cached) return cached

Expand Down Expand Up @@ -81,7 +90,7 @@ export const imageDimensions = async (url: string | undefined | null, noCache?:
export const imageDimensionsFromFile = async (file: string, noCache?: boolean) => {
if (!file) return null

const cacheKey = (!noCache && encodeURIComponent(file)) || null
const cacheKey = await genCacheKey(file, noCache)
const cached = getCache<Dimensions>(cacheKey)
if (cached) return cached

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@tryghost/content-api": "^1.5.1",
"@types/refractor": "^3.0.0",
"cheerio": "^1.0.0-rc.5",
"crypto-hash": "^1.3.0",
"dayjs": "^1.10.4",
"disqus-react": "^1.0.11",
"email-validator": "^2.0.4",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,11 @@ crypto-browserify@3.12.0, crypto-browserify@^3.11.0:
randombytes "^2.0.0"
randomfill "^1.0.3"

crypto-hash@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/crypto-hash/-/crypto-hash-1.3.0.tgz#b402cb08f4529e9f4f09346c3e275942f845e247"
integrity sha512-lyAZ0EMyjDkVvz8WOeVnuCPvKVBXcMv1l5SVqO1yC7PzTwrD/pPje/BIRbWhMoPe436U+Y2nD7f5bFx0kt+Sbg==

css-blank-pseudo@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/css-blank-pseudo/-/css-blank-pseudo-0.1.4.tgz#dfdefd3254bf8a82027993674ccf35483bfcb3c5"
Expand Down

1 comment on commit 3159f72

@vercel
Copy link

@vercel vercel bot commented on 3159f72 Apr 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.