-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Snapshot from scm.treehouse.uc/fs-v source correlation id:bbe05221-7d…
…31-4f3d-ae69-bd652043f4bc; job:5fbafb1d-f36d-4ea2-8af0-a3b72688bb75
- Loading branch information
1 parent
4ac6444
commit cb866fa
Showing
13 changed files
with
610 additions
and
65 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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 |
---|---|---|
@@ -1,24 +1,22 @@ | ||
declare module "dbus-native" { | ||
type EventHandler = () => void; | ||
|
||
type EventHandler = () => void; | ||
interface Interface { | ||
on(eventName: string, handler: EventHandler): void; | ||
} | ||
|
||
interface Interface { | ||
on(eventName: string, handler: EventHandler): void; | ||
type GetInterfaceCallback<T extends Interface> = (error: Error, interface: T) => void; | ||
|
||
Queue(): void; | ||
Dequeue(): void; | ||
GetSupported(): void; | ||
GetFlavors(): void; | ||
} | ||
interface Service { | ||
getInterface<T extends Interface>( | ||
path: string, | ||
id: string, | ||
callback: GetInterfaceCallback<T>): void; | ||
} | ||
|
||
type GetInterfaceCallback = (error: Error, interface: Interface) => void; | ||
interface SessionBus { | ||
getService(id: string): Service; | ||
} | ||
|
||
interface Service { | ||
getInterface(path: string, id: string, callback: GetInterfaceCallback): void; | ||
} | ||
|
||
interface SessionBus { | ||
getService(id: string): Service; | ||
} | ||
|
||
export function sessionBus(): SessionBus; | ||
export function sessionBus(): SessionBus; | ||
} |
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 |
---|---|---|
@@ -1,24 +1,155 @@ | ||
import {app, protocol, FilePathWithHeaders, Request} from "electron"; | ||
import path from "path"; | ||
import {createHash} from "crypto"; | ||
import {sessionBus, Interface} from "dbus-native"; | ||
import {getType as getMimeType} from "mime"; | ||
import {join as joinPath} from "path"; | ||
import {setTimeout} from "timers"; | ||
|
||
const homePath = app.getPath("home"); | ||
interface DequeueRequest { | ||
handle: number; | ||
} | ||
|
||
type QueueCallback = (err: Error, handle: number) => void; | ||
|
||
type ReadySignalHandler = (handle: number, uris: string[]) => void; | ||
|
||
type ErrorSignalHandler = (handle: number, uris: string[], errorCode: number, message: string) => void; | ||
|
||
interface Thumbnailer extends Interface { | ||
GetSupported(): void; | ||
GetSchedulers(): void; | ||
GetFlavors(): void; | ||
Dequeue(request: DequeueRequest): void; | ||
Queue( | ||
uris: string[], | ||
mimeTypes: string[], | ||
flavor: "normal" | string, | ||
scheduler: "default" | string, | ||
handleToDequeue: 0 | number, | ||
cb: QueueCallback): void; | ||
|
||
on(event: "Ready", handler: ReadySignalHandler): void; | ||
on(event: "Error", handler: ErrorSignalHandler): void; | ||
} | ||
|
||
const resolutionMapping: {[k in ThumbnailResolution]: string} = { | ||
"default": "normal", | ||
"high": "large", | ||
}; | ||
|
||
type RequestCallback = (path: string | FilePathWithHeaders) => void; | ||
|
||
interface Batch { | ||
paths: string[]; | ||
mimeTypes: string[]; | ||
callbacks: RequestCallback[]; | ||
} | ||
|
||
function createBatch(): Batch { | ||
return { | ||
paths: [], | ||
mimeTypes: [], | ||
callbacks: [], | ||
}; | ||
} | ||
|
||
interface PendingRequest { | ||
resolution: string; | ||
callback: RequestCallback; | ||
} | ||
|
||
function hashMD5(text: string): string { | ||
return createHash("md5").update(text).digest("hex"); | ||
const pendingRequests: {[uri: string]: PendingRequest} = {}; | ||
let unsubmitted: Batch = createBatch(); | ||
let batchTimer: NodeJS.Timeout | null = null; | ||
|
||
function submitBatch(thumbnailer: Thumbnailer, size: ThumbnailResolution) { | ||
batchTimer = null; | ||
const batch = unsubmitted; | ||
unsubmitted = createBatch(); | ||
const resolution = resolutionMapping[size]; | ||
|
||
function callback(err: Error | null, handle: number) { | ||
if (err) | ||
console.error("Unable to submit thumbnailing request", err); | ||
else for (let n = batch.callbacks.length; n --> 0;) | ||
pendingRequests[batch.paths[n]] = { | ||
resolution, | ||
callback: batch.callbacks[n], | ||
}; | ||
} | ||
|
||
thumbnailer.Queue( | ||
batch.paths, | ||
batch.mimeTypes, | ||
resolution, | ||
"foreground", | ||
0, | ||
callback); | ||
} | ||
|
||
function handleThumbnailRequest( | ||
thumbnailer: Thumbnailer, | ||
request: Request, | ||
done: (response: FilePathWithHeaders) => void, | ||
callback: RequestCallback, | ||
): void { | ||
let requestPath = request.url.slice(8); // len("thumb://") | ||
requestPath = path.normalize(`${__dirname}/${requestPath}`); | ||
const suffixIndex = request.url.indexOf("?", 8); | ||
const size = suffixIndex > 0 && request.url.slice(suffixIndex + 3); | ||
const path = request.url.slice(8 /* len("thumb://") */, suffixIndex); | ||
const mimeType = getMimeType(path); | ||
if (!mimeType) return callback(""); | ||
|
||
unsubmitted.paths.push(`file://${path}`); | ||
unsubmitted.mimeTypes.push(mimeType); | ||
unsubmitted.callbacks.push(callback); | ||
|
||
let file = hashMD5(`file://${requestPath}`); | ||
done({ path: path.join(homePath, `.cache/thumbnails/normal/${file}.png`) }); | ||
if (batchTimer == null) | ||
batchTimer = setTimeout(submitBatch, 500, thumbnailer, size); | ||
} | ||
|
||
export function registerThumbnailProtocol(): void { | ||
protocol.registerFileProtocol("thumb", handleThumbnailRequest); | ||
function handleResponse( | ||
handle: number, | ||
uris: string[], | ||
process: (uri: string, pr: PendingRequest) => void, | ||
) { | ||
for (let n = uris.length; n --> 0;) { | ||
let path = uris[n]; | ||
|
||
const pr = pendingRequests[path]; | ||
if (!pr) continue | ||
|
||
process(path, pr); | ||
} | ||
} | ||
|
||
const homePath = app.getPath("home"); | ||
|
||
function handleReady(handle: number, uris: string[]): void { | ||
handleResponse(handle, uris, (uri, pr) => { | ||
const hash = createHash("md5").update(uri).digest("hex"); | ||
const path = `.cache/thumbnails/${pr.resolution}/${hash}.png`; | ||
pr.callback(joinPath(homePath, path)); | ||
}); | ||
} | ||
|
||
function handleError(handle: number, uris: string[], errorCode: number, message: string): void { | ||
handleResponse(handle, uris, (uri, pr) => pr.callback("")); | ||
} | ||
|
||
export function registerThumbnailProtocol(): void { | ||
sessionBus() | ||
.getService("org.freedesktop.thumbnails.Thumbnailer1") | ||
.getInterface<Thumbnailer>( | ||
"/org/freedesktop/thumbnails/Thumbnailer1", | ||
"org.freedesktop.thumbnails.Thumbnailer1", | ||
(err, thumbnailer) => { | ||
if (err) { | ||
console.error("Thumbnailer startup failure", err); | ||
} else { | ||
thumbnailer.on("Error", handleError); | ||
thumbnailer.on("Ready", handleReady); | ||
|
||
const handler = handleThumbnailRequest.bind(null, thumbnailer); | ||
protocol.registerFileProtocol("thumb", handler); | ||
} | ||
}); | ||
} |
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.