This repository was archived by the owner on May 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
A sample implementation of a resource fetcher plugin. This plugin of… #350
Open
kimtuck
wants to merge
1
commit into
readium:develop
Choose a base branch
from
kimtuck:rdh-ResourceFetcherPluginSample
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,103 @@ | ||
|
|
||
| // This is a sample implementation of a resource fetcher plugin. It is intended to show how a resource fetcher plugin might be written. | ||
| // This plugin uses an ajax call to call to a service to get the contents of a resource. | ||
| // | ||
| // It's based on a plugin used at Bibliotheca to get the contents of an epub from a server. I've edited this file to remove | ||
| // some functionality specific to Bibliotheca. The result may or may not be suitiable for your needs. | ||
|
|
||
| define(['readium_js_plugins', 'readium_shared_js/globals', '../../../js/epub-fetch/discover_content_type' ], | ||
| function (Plugins, Globals,ContentTypeDiscovery) { | ||
| var config = {}; | ||
| Plugins.register("ResourceFetcherPlugin", function (api) { | ||
|
|
||
| api.plugin.CustomResourceFetcher = function(parentFetcher){ | ||
| var ebookURL = parentFetcher.getEbookURL(); | ||
| var ebookURL_filepath = parentFetcher.getEbookURL_FilePath(); | ||
|
|
||
| var self = this; | ||
|
|
||
| // INTERNAL FUNCTIONS | ||
| // PUBLIC API | ||
| this.shouldConstructDomProgrammatically = function() { return true; } | ||
| this.resolveURI = function (pathRelativeToPackageRoot) { | ||
| var pathRelativeToPackageRootUri = undefined; | ||
| try { | ||
| pathRelativeToPackageRootUri = new URI(pathRelativeToPackageRoot); | ||
| } catch(err) { | ||
| console.error(err); | ||
| console.log(pathRelativeToPackageRoot); | ||
| } | ||
| if (pathRelativeToPackageRootUri && pathRelativeToPackageRootUri.is("absolute")) return pathRelativeToPackageRoot; //pathRelativeToPackageRootUri.scheme() == "http://", "https://", "data:", etc. | ||
|
|
||
|
|
||
| var url = ebookURL_filepath; | ||
| try { | ||
| //url = new URI(relativeUrl).absoluteTo(url).search('').hash('').toString(); | ||
| url = new URI(url).search('').hash('').toString(); | ||
| } catch(err) { | ||
| console.error(err); | ||
| console.log(url); | ||
| } | ||
|
|
||
| return url + (url.charAt(url.length-1) == '/' ? "" : "/") + pathRelativeToPackageRoot; | ||
| }; | ||
|
|
||
| this.fetchFileContents = function(pathRelativeToPackageRoot, isText, fetchCallback, onerror) { | ||
| var body={}; | ||
| var config={ }; | ||
|
|
||
| var success = function(data, textStatus, jqXHR) { | ||
| ReadiumSDK.emit('ResourceFetcher-Success', data, pathRelativeToPackageRoot, body, config); | ||
| fetchCallback(data); | ||
| }; | ||
|
|
||
| var error = function (xhr, status, errorThrown, self) { | ||
| onerror(new Error(errorThrown)); | ||
| } | ||
|
|
||
| // Hook; someone can listen for this event and modify the config object before the ajax call. | ||
| ReadiumSDK.emit('ResourceFetcher-BeforeFetchFileContents', pathRelativeToPackageRoot, isText, config, success,error); | ||
| config.isText=isText; | ||
|
|
||
| var ajaxArguments ={ | ||
| retryCount: 0, | ||
| isText: isText, | ||
| headers: { | ||
| 'Accept': 'application/json', | ||
| 'Content-Type': 'application/json' | ||
| }, | ||
| url: config.url, | ||
| type: 'POST', | ||
| dataType: 'json', | ||
| data: JSON.stringify(body), | ||
| success: success, | ||
| error: error | ||
| } | ||
|
|
||
| $.ajax(ajaxArguments); | ||
| }; | ||
|
|
||
| this.fetchFileContentsText = function(pathRelativeToPackageRoot, fetchCallback, onerror) { | ||
| this.fetchFileContents(pathRelativeToPackageRoot, true, function (result) { | ||
| fetchCallback(result); | ||
| }, onerror); | ||
| }; | ||
|
|
||
| this.fetchFileContentsBlob = function(pathRelativeToPackageRoot, fetchCallback, onerror) { | ||
| this.fetchFileContents(pathRelativeToPackageRoot, false, function(result) { | ||
| var contentType = ContentTypeDiscovery.identifyContentTypeFromFileName(pathRelativeToPackageRoot); | ||
| var bytes = new Uint8Array(result.length); for (var i=0; i<result.length; i++) bytes[i] = result.charCodeAt(i); | ||
| var blob = new Blob([bytes],{type: contentType}); | ||
| fetchCallback(blob); | ||
| },onerror); | ||
| }; | ||
|
|
||
| }; | ||
| api.plugin.CustomResourceFetcher.openPackageDocument = function(ebookURL, callback, openPageRequest, openPackageDocument_) { | ||
| openPackageDocument_(ebookURL, callback, openPageRequest, ''); | ||
| } | ||
| }); | ||
|
|
||
| return config; | ||
| }); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
../../../js/epub-fetch/discover_content_typehttps://github.com/readium/readium-js/blob/develop/js/epub-fetch/discover_content_type.js
This reference is problematic, because code in
readium-shared-jscannot make assumptions about its integration context. This dependency injection assumes thatreadium-jsis the parent repository (thatreadium-shared-jsis a submodule). ReadiumSDK-based native apps also make use ofreadium-shared-js, but have absolutely zero-awareness ofreadium-js.Now, this issue is mitigated by the fact that in practice, the
ResourceFetcherPluginwould only ever be used in the context of an app that relies onreadium-js. But generally-speaking, there is definitely a problem inreadium-shared-jsplugins having dependencies from external RequireJS namespaces (../../../js/epub-fetch/discover_content_typeis of course equivalent toreadium_js/epub-fetch/discover_content_type, but this syntax would only work when building fromreadium-jsorreadium-js-viewer, not from withinreadium-shared-js).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PS: the point I am making here is that Readium "plugins" were originally designed (minimally) for usage within the
readium-shared-jsscope. So some architectural hacks are required to implement plugins forreadium-js, etc.@jccr