-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: universal meta resolving (#55)
Co-authored-by: Ahad Birang <[email protected]>
- Loading branch information
Showing
11 changed files
with
297 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { RuntimeImageInfo } from 'types' | ||
|
||
export async function getMeta (url, cache): RuntimeImageInfo { | ||
const cacheKey = 'image:meta:' + url | ||
if (cache.has(cacheKey)) { | ||
return cache.get(cacheKey) | ||
} | ||
|
||
if (process.client) { | ||
if (typeof Image === 'undefined') { | ||
throw new TypeError('Image not supported') | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
const img = new Image() | ||
img.onload = () => { | ||
const meta = { | ||
width: img.width, | ||
height: img.height, | ||
placeholder: url | ||
} | ||
cache.set(cacheKey, meta) | ||
resolve(meta) | ||
} | ||
img.onerror = err => reject(err) | ||
img.src = url | ||
}) | ||
} | ||
|
||
if (process.server) { | ||
const imageMeta = require('image-meta').default | ||
const data: Buffer = await fetch(url).then((res: any) => res.buffer()) | ||
const { width, height, mimeType } = await imageMeta(data) | ||
const meta = { | ||
width, | ||
height, | ||
placeholder: `data:${mimeType};base64,${data.toString('base64')}` | ||
} | ||
cache.set(cacheKey, meta) | ||
return meta | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
"types": [ | ||
"@nuxt/types", | ||
"@types/node", | ||
"image-meta", | ||
"./types" | ||
], | ||
"paths": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.