Skip to content

Commit

Permalink
rm unused sharp code
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Sep 22, 2023
1 parent cdc36db commit a6b4b4f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 312 deletions.
82 changes: 80 additions & 2 deletions packages/pds/src/image/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,80 @@
export * from './sharp'
export type { Options, ImageInfo } from './util'
import { Readable } from 'stream'
import { pipeline } from 'stream/promises'
import sharp from 'sharp'
import { errHasMsg } from '@atproto/common'

export async function maybeGetInfo(
stream: Readable,
): Promise<ImageInfo | null> {
let metadata: sharp.Metadata
try {
const processor = sharp()
const [result] = await Promise.all([
processor.metadata(),
pipeline(stream, processor), // Handles error propagation
])
metadata = result
} catch (err) {
if (errHasMsg(err, 'Input buffer contains unsupported image format')) {
return null
}
throw err
}
const { size, height, width, format } = metadata
if (
size === undefined ||
height === undefined ||
width === undefined ||
format === undefined
) {
return null
}

return {
height,
width,
size,
mime: formatsToMimes[format] ?? ('unknown' as const),
}
}

export async function getInfo(stream: Readable): Promise<ImageInfo> {
const maybeInfo = await maybeGetInfo(stream)
if (!maybeInfo) {
throw new Error('could not obtain all image metadata')
}
return maybeInfo
}

export type Options = Dimensions & {
format: 'jpeg' | 'png'
// When 'cover' (default), scale to fill given dimensions, cropping if necessary.
// When 'inside', scale to fit within given dimensions.
fit?: 'cover' | 'inside'
// When false (default), do not scale up.
// When true, scale up to hit dimensions given in options.
// Otherwise, scale up to hit specified min dimensions.
min?: Dimensions | boolean
// A number 1-100
quality?: number
}

export type ImageInfo = Dimensions & {
size: number
mime: `image/${string}` | 'unknown'
}

export type Dimensions = { height: number; width: number }

export const formatsToMimes: {
[s in keyof sharp.FormatEnum]?: `image/${string}`
} = {
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
png: 'image/png',
gif: 'image/gif',
svg: 'image/svg+xml',
tif: 'image/tiff',
tiff: 'image/tiff',
webp: 'image/webp',
}
93 changes: 0 additions & 93 deletions packages/pds/src/image/sharp.ts

This file was deleted.

32 changes: 0 additions & 32 deletions packages/pds/src/image/util.ts

This file was deleted.

185 changes: 0 additions & 185 deletions packages/pds/tests/image/sharp.test.ts

This file was deleted.

0 comments on commit a6b4b4f

Please sign in to comment.