Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22
11 changes: 10 additions & 1 deletion apps/web/src/reader/streamer/archives.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,16 @@ export const isPdfFile = (
)
}

const getEncodingFormat = (
file: NonNullable<PromiseReturnType<typeof getBookFile>>,
) => (file.data.type.length > 0 ? file.data.type : undefined)

export const getArchiveForZipFile = async (
file: NonNullable<PromiseReturnType<typeof getBookFile>>,
): Promise<Archive> => {
try {
const normalizedName = file.data.name.toLowerCase()
const encodingFormat = getEncodingFormat(file)

if (
isPotentialZipFile({ name: file.data.name, mimeType: file.data.type })
Expand All @@ -68,6 +73,7 @@ export const getArchiveForZipFile = async (
return createArchiveFromJszip(jszip, {
orderByAlpha: true,
name: file.data.name,
encodingFormat,
})
} catch (e) {
Logger.error(
Expand All @@ -80,7 +86,9 @@ export const getArchiveForZipFile = async (
}

if (normalizedName.endsWith(`.txt`)) {
return createArchiveFromText(file.data)
return createArchiveFromText(file.data, {
mimeType: encodingFormat ?? "text/plain",
})
}

throw new StreamerFileNotSupportedError(`FileNotSupportedError`)
Expand All @@ -107,5 +115,6 @@ export const getArchiveForRarFile = async (
return createArchiveFromLibArchive(archive, {
orderByAlpha: true,
name: file.data.name,
encodingFormat: getEncodingFormat(file),
})
}
30 changes: 22 additions & 8 deletions apps/web/src/reader/streamer/manifestHooks.shared.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,26 @@ const createManifest = ({
})

describe("readingDirectionManifestHook", () => {
it("defaults CBZ archives to RTL before spine hooks run", async () => {
it("applies an explicit direction directive over the current manifest value", async () => {
const manifest = await readingDirectionManifestHook({
archive: createArchive("Book.CBZ"),
archive: createArchive("Book [oboku~direction~ltr].cbz"),
baseUrl: "",
})(createManifest())
})(createManifest({ readingDirection: "rtl" }))

expect(manifest.readingDirection).toBe("rtl")
expect(manifest.readingDirection).toBe("ltr")
})

it("uses explicit direction directives before CBZ defaults", async () => {
it("returns the manifest unchanged when no direction directive is present", async () => {
const input = createManifest({
filename: "Book.cbz",
readingDirection: undefined,
})
const manifest = await readingDirectionManifestHook({
archive: createArchive("Book [oboku~direction~ltr].cbz"),
archive: createArchive("Book.cbz"),
baseUrl: "",
})(createManifest({ readingDirection: "rtl" }))
})(input)

expect(manifest.readingDirection).toBe("ltr")
expect(manifest).toBe(input)
})

it("preserves existing direction for non-CBZ archives without directives", async () => {
Expand All @@ -61,6 +65,16 @@ describe("readingDirectionManifestHook", () => {

expect(manifest.readingDirection).toBe("rtl")
})

it("returns the manifest unchanged when the directive matches the current value", async () => {
const input = createManifest({ readingDirection: "ltr" })
const manifest = await readingDirectionManifestHook({
archive: createArchive("Book [oboku~direction~ltr].cbz"),
baseUrl: "",
})(input)

expect(manifest).toBe(input)
})
})

describe("webtoonManifestHook", () => {
Expand Down
27 changes: 4 additions & 23 deletions apps/web/src/reader/streamer/manifestHooks.shared.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
import { directives } from "@oboku/shared"
import type {
Archive,
Manifest,
StreamerManifestHookFactory,
} from "@prose-reader/streamer"

type SpineItem = Manifest["spineItems"][number]

const isCbzArchive = (archive: Archive) =>
archive.filename.toLowerCase().endsWith(".cbz")

const getReadingDirection = ({
archive,
manifest,
}: {
archive: Archive
manifest: Manifest
}): Manifest["readingDirection"] => {
const { direction } = directives.extractDirectivesFromName(archive.filename)

return (
direction ?? (isCbzArchive(archive) ? "rtl" : manifest.readingDirection)
)
}

const setWebtoonRenditionLayout = (spineItem: SpineItem): SpineItem => ({
...spineItem,
renditionLayout: "reflowable",
Expand All @@ -32,15 +14,14 @@ const setWebtoonRenditionLayout = (spineItem: SpineItem): SpineItem => ({
export const readingDirectionManifestHook: StreamerManifestHookFactory =
({ archive }) =>
(manifest) => {
const readingDirection = getReadingDirection({ archive, manifest })
const { direction } = directives.extractDirectivesFromName(archive.filename)

if (readingDirection === manifest.readingDirection) {
return manifest
}
if (direction === undefined) return manifest
if (direction === manifest.readingDirection) return manifest

return {
...manifest,
readingDirection,
readingDirection: direction,
}
}

Expand Down
5 changes: 4 additions & 1 deletion apps/web/src/reader/streamer/swStreamer.sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import {
export const swStreamer = new ServiceWorkerStreamer({
hooks: {
manifest: {
content: [readingDirectionManifestHook],
content: [
readingDirectionManifestHook,
...cbzStreamerHooks.manifest.content,
],
spine: cbzStreamerHooks.manifest.spine,
presentation: [webtoonManifestHook],
},
Expand Down
5 changes: 4 additions & 1 deletion apps/web/src/reader/streamer/webStreamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ pdfjsLib.GlobalWorkerOptions.workerSrc = new URL(
export const webStreamer = new Streamer({
hooks: {
manifest: {
content: [readingDirectionManifestHook],
content: [
readingDirectionManifestHook,
...cbzStreamerHooks.manifest.content,
],
spine: cbzStreamerHooks.manifest.spine,
presentation: [webtoonManifestHook],
},
Expand Down
Loading
Loading