Skip to content

Instantly share code, notes, and snippets.

@chewtoys
Created October 20, 2020 01:56
Show Gist options
  • Save chewtoys/ac11b0fb70e766578923b7834d137602 to your computer and use it in GitHub Desktop.
Save chewtoys/ac11b0fb70e766578923b7834d137602 to your computer and use it in GitHub Desktop.
// https://www.twitch.tv/videos/775515078
import fs from 'fs'
import { Asset, AssetType } from ''
import { Upload } from ''
import { encode } from 'blurhash'
import sharp from 'sharp'
const MAX_WIDTH = 1000
const MAX_HEIGHT = 1000
type Options = {
prefix?: string
maxWidth?: number
maxHeight?: number
generatePreview?: boolean
}
export async function uploadPhoto(
file: Upload,
{
prefix = '',
maxHeight = MAX_HEIGHT,
maxWidth = MAX_WIDTH,
generatePreview,
}: Options = {},
) {
const asset = new Asset(AssetType.PHOTO)
const pipeline = sharp()
.flatten({ background: { r: 255, g: 255, b: 255 } })
.resize(maxWidth, maxHeight, { fit: 'inside' })
fs.createReadStream(file.path).pipe(pipeline)
const outputstream = pipeline.clone().toFormat('jpeg')
const promises: Promise<any>[] = []
promises.push(
//TODO upload
upload({
Body: outputstream,
})
.promise(),
)
if (generatePreview) {
promises.push(
new Promise((resolve, reject) => {
pipeline
.clone()
.raw()
.ensureAlpha()
.toBuffer((err, buffer, { width, height }) => {
if (err) return reject(err)
asset.preview = encode(
new Uint8ClampedArray(buffer),
width,
height,
4,
4,
)
resolve(null)
})
})
}
await Promise.all(promises)
return asset
}
async function uploadImage(file: Upload, { generatePreview }: Options) {
//TODO Upload
const asset = await Image.Assets.create({
input: 'http://img'
})
return new Asset(AssetType.IMAGE)
}
export function upload(file: Upload, options: Options = {}): Promise<Asset> {
//TODO upload
return uploadImage(file, options)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment