-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
319 additions
and
99 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 was deleted.
Oops, something went wrong.
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,49 @@ | ||
import { | ||
createReadableStreamFromReadable, | ||
writeReadableStreamToWritable, | ||
} from '@remix-run/node'; | ||
import { createReadStream, createWriteStream } from 'node:fs'; | ||
import { dirname } from 'node:path'; | ||
import { ImageError, type ImageRecord, type ImageStream } from './types'; | ||
import { safeStat } from './utils'; | ||
import { getLegacyImageStream } from './legacy'; | ||
import { stat, mkdir, rename } from 'node:fs/promises'; | ||
import { createOptimized } from './optimizer'; | ||
import { getImagePath, getPreviewPath, getThumbnailPath } from './paths'; | ||
|
||
export async function ensureImage(image: ImageRecord) { | ||
const path = getImagePath(image); | ||
let stats = await safeStat(path); | ||
if (stats != null) return stats; | ||
const imageStream = await getLegacyImageStream(image); | ||
await mkdir(dirname(path), { recursive: true }); | ||
await writeReadableStreamToWritable( | ||
imageStream.stream, | ||
createWriteStream(path) | ||
); | ||
stats = await safeStat(path); | ||
if (stats == null) throw new ImageError('image', image.id); | ||
return stats; | ||
} | ||
|
||
export async function getImageStream( | ||
image: ImageRecord, | ||
ensure = false | ||
): Promise<ImageStream> { | ||
const path = getImagePath(image); | ||
const stats = ensure ? await ensureImage(image) : await stat(path); | ||
const stream = createReadableStreamFromReadable(createReadStream(path)); | ||
return { | ||
id: image.id, | ||
stream, | ||
mimetype: image.mimetype ?? 'application/octet-stream', | ||
size: stats.size, | ||
}; | ||
} | ||
|
||
export async function commitUpload(stagePath: string, image: ImageRecord) { | ||
const path = getImagePath(image); | ||
await mkdir(dirname(path), { recursive: true }); | ||
await rename(stagePath, path); | ||
createOptimized(path, getThumbnailPath(image), getPreviewPath(image)); | ||
} |
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,18 @@ | ||
import { ImageError, type ImageRecord, type ImageStream } from './types'; | ||
|
||
export async function getLegacyImageStream( | ||
image: ImageRecord | ||
): Promise<ImageStream> { | ||
const id = image.legacy_id; | ||
if (!id) throw new ImageError('legacy', 'missing'); | ||
const res = await fetch( | ||
`https://dfoto.se/v1/image/${image.legacy_id}/fullSize` | ||
); | ||
if (!res.ok || !res.body) throw new ImageError('legacy', id); | ||
return { | ||
id: image.id, | ||
stream: res.body, | ||
mimetype: image.mimetype ?? 'application/octet-stream', | ||
size: null, | ||
}; | ||
} |
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,31 @@ | ||
import sharp from 'sharp'; | ||
|
||
export async function createThumbnail(input: string, output: string) { | ||
await sharp(input) | ||
.resize(300, 200, { fit: 'cover', position: sharp.strategy.entropy }) | ||
.rotate() | ||
.webp() | ||
.toFile(output); | ||
} | ||
|
||
export async function createPreview(input: string, output: string) { | ||
await sharp(input) | ||
.resize(1200, 800, { fit: 'inside' }) | ||
.rotate() | ||
.webp() | ||
.toFile(output); | ||
} | ||
|
||
export async function createOptimized( | ||
input: string, | ||
thumbnailOutput: string, | ||
previewOutput: string | ||
) { | ||
const pipeline = sharp(input).rotate().webp().clone(); | ||
await Promise.all([ | ||
pipeline | ||
.resize(300, 200, { fit: 'cover', position: sharp.strategy.entropy }) | ||
.toFile(thumbnailOutput), | ||
pipeline.resize(null, 800).toFile(previewOutput), | ||
]); | ||
} |
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,31 @@ | ||
import { extension } from 'mime-types'; | ||
import { join } from 'node:path'; | ||
|
||
export const storagePath = process.env.STORAGE_PATH ?? './storage'; | ||
export const imagePath = join(storagePath, 'images'); | ||
export const thumbnailPath = join(storagePath, 'thumbnails'); | ||
export const previewPath = join(storagePath, 'previews'); | ||
export const uploadsPath = join(storagePath, 'uploads'); | ||
|
||
export function getImagePath(image: { | ||
id: number; | ||
album_id: number; | ||
mimetype?: string | null; | ||
}): string { | ||
const ext = image.mimetype ? extension(image.mimetype) || '' : ''; | ||
return join(imagePath, image.album_id.toString(), `${image.id}.${ext}`); | ||
} | ||
|
||
export function getThumbnailPath(image: { | ||
id: number; | ||
album_id: number; | ||
}): string { | ||
return join(thumbnailPath, image.album_id.toString(), `${image.id}.webp`); | ||
} | ||
|
||
export function getPreviewPath(image: { | ||
id: number; | ||
album_id: number; | ||
}): string { | ||
return join(previewPath, image.album_id.toString(), `${image.id}.webp`); | ||
} |
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,37 @@ | ||
import { createReadStream } from 'node:fs'; | ||
import { ImageError, type ImageRecord, type ImageStream } from './types'; | ||
import { safeStat } from './utils'; | ||
import { createReadableStreamFromReadable } from '@remix-run/node'; | ||
import { createPreview } from './optimizer'; | ||
import { stat, mkdir } from 'node:fs/promises'; | ||
import { getImagePath, getPreviewPath } from './paths'; | ||
import { ensureImage } from './image'; | ||
import { dirname } from 'node:path'; | ||
|
||
async function ensurePreview(image: ImageRecord) { | ||
const path = getPreviewPath(image); | ||
let stats = await safeStat(path); | ||
if (stats != null) return stats; | ||
await ensureImage(image); | ||
const imagePath = getImagePath(image); | ||
mkdir(dirname(path), { recursive: true }); | ||
await createPreview(imagePath, path); | ||
stats = await safeStat(path); | ||
if (stats == null) throw new ImageError('image', image.id); | ||
return stats; | ||
} | ||
|
||
export async function getPreviewStream( | ||
image: ImageRecord, | ||
ensure = false | ||
): Promise<ImageStream> { | ||
const path = getPreviewPath(image); | ||
const stats = ensure ? await ensurePreview(image) : await stat(path); | ||
const stream = createReadableStreamFromReadable(createReadStream(path)); | ||
return { | ||
id: image.id, | ||
stream, | ||
mimetype: 'image/webp', | ||
size: stats.size, | ||
}; | ||
} |
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,37 @@ | ||
import { createReadStream } from 'node:fs'; | ||
import { ImageError, type ImageRecord, type ImageStream } from './types'; | ||
import { safeStat } from './utils'; | ||
import { createReadableStreamFromReadable } from '@remix-run/node'; | ||
import { ensureImage } from './image'; | ||
import { createThumbnail } from './optimizer'; | ||
import { stat, mkdir } from 'node:fs/promises'; | ||
import { getImagePath, getThumbnailPath } from './paths'; | ||
import { dirname } from 'node:path'; | ||
|
||
async function ensureThumbnail(image: ImageRecord) { | ||
const path = getThumbnailPath(image); | ||
let stats = await safeStat(path); | ||
if (stats != null) return stats; | ||
await ensureImage(image); | ||
const imagePath = getImagePath(image); | ||
mkdir(dirname(path), { recursive: true }); | ||
await createThumbnail(imagePath, path); | ||
stats = await safeStat(path); | ||
if (stats == null) throw new ImageError('image', image.id); | ||
return stats; | ||
} | ||
|
||
export async function getThumbnailStream( | ||
image: ImageRecord, | ||
ensure = false | ||
): Promise<ImageStream> { | ||
const path = getThumbnailPath(image); | ||
const stats = ensure ? await ensureThumbnail(image) : await stat(path); | ||
const stream = createReadableStreamFromReadable(createReadStream(path)); | ||
return { | ||
id: image.id, | ||
stream, | ||
mimetype: 'image/webp', | ||
size: stats.size, | ||
}; | ||
} |
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,24 @@ | ||
export class ImageError extends Error { | ||
type: 'legacy' | 'image'; | ||
id: string | number; | ||
constructor(type: 'legacy' | 'image', id: string | number) { | ||
super(`Failed to fetch ${type} ${id}`); | ||
this.name = 'ImageError'; | ||
this.id = id; | ||
this.type = type; | ||
} | ||
} | ||
|
||
export interface ImageStream { | ||
id: number; | ||
stream: ReadableStream; | ||
mimetype: string | null; | ||
size: number | null; | ||
} | ||
|
||
export interface ImageRecord { | ||
id: number; | ||
album_id: number; | ||
mimetype?: string | null; | ||
legacy_id?: string | null; | ||
} |
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,9 @@ | ||
import { stat } from 'node:fs/promises'; | ||
|
||
export async function safeStat(path: string) { | ||
try { | ||
return await stat(path); | ||
} catch (e) { | ||
return null; | ||
} | ||
} |
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.