2 * This file is part of WolneLektury-Mobile, licensed under GNU Affero GPLv3 or later.
3 * Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 var FileRepo = new function() {
7 /* API for files repository */
11 this.init = function(success, error) {
12 self.initRoot(success);
15 this.initRoot = function(success) {
16 // fs size is irrelevant, PERSISTENT is futile (on Android, at least)
17 window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fs) {
18 console.log('local fs found: ' + fs.root.fullPath);
22 console.log('local fs not found');
28 this.withLocalHtml = function(book_id, success, error) {
29 console.log('info:withLocalHtml: id:' + book_id);
30 View.spinner('Otwieranie treści utworu');
32 error && error('info:withLocalHtml: no local html: no usable filesystem');
34 var url = "file://" + self.root.fullPath + "/html/" + book_id;
35 console.log('info:withLocalHtml: local ajax: ' + url);
36 var xhr = new XMLHttpRequest();
37 xhr.open('GET', url, true);
38 xhr.onload = function() {
39 console.log('info:withLocalHtml: fetched by local ajax: ' + url);
40 success && success(xhr.responseText);
47 this.withLocal = function(win, fail) {
49 self.root.getDirectory('html', {}, function(dir) {
50 var reader = dir.createReader();
51 reader.readEntries(win, fail);
60 // downloads HTML file from server, saves it in cache and calls success with file contents
61 this.withHtmlFromServer = function(book_id, success, error) {
62 console.log('info:withHtmlFromServer: id:' + book_id);
64 Catalogue.withBook(book_id, function(book) {
65 var url = WL + book.html_file;
66 console.log('info:withHtmlFromServer: fetching url: ' + url);
68 View.spinner("Pobieranie treści utworu z sieci");
71 Downloader.downloadFile(url, self.root.fullPath + "/html/", ""+book_id, true,
73 console.log('info:withHtmlFromServer: loaded file from WL');
74 self.withLocalHtml(book_id, success, error);
76 console.log('error downloading file!')
77 error && error("error: " + data);
81 // there's no big fs, so we'll just get the text from AJAX
82 console.log('info:withHtmlFromServer: ajax: ' + url);
83 var xhr = new XMLHttpRequest();
85 xhr.onload = function() {
86 console.log('info:withHtmlFromServer: fetched by ajax: ' + url);
87 success && success(xhr.responseText);
89 xhr.onerror = function() {
90 console.log('error downloading file!')
91 error && error("error: " + data);
98 // calls the callback with contents of the HTML file for a given book,
99 // loaded from the server and cached locally
100 this.withHtml = function(id, success, error) {
101 console.log('info:withHtml: id:' + id);
102 self.withLocalHtml(id, success, function() {
103 self.withHtmlFromServer(id, success, error);
108 this.clear = function() {
109 FileRepo.withLocal(function(local) {
117 this.deleteIfExists = function(id) {
119 self.root.getFile('html/' + id, {create: false}, function(f) {