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 debug('local fs found: ' + fs.root.fullPath);
23 debug('local fs not found');
29 this.withLocalHtml = function(book_id, success, error) {
30 debug('info:withLocalHtml: id:' + book_id);
31 View.spinner('Otwieranie treści utworu');
33 error && error('info:withLocalHtml: no local html: no usable filesystem');
35 var url = "file://" + self.root.fullPath + "/html/" + book_id;
36 debug('info:withLocalHtml: local ajax: ' + url);
37 var xhr = new XMLHttpRequest();
38 xhr.open('GET', url, true);
39 xhr.onload = function() {
40 debug('info:withLocalHtml: fetched by local ajax: ' + url);
41 success && success(xhr.responseText);
48 this.withLocal = function(win, fail) {
50 self.root.getDirectory('html', {create:true}, function(dir) {
51 var reader = dir.createReader();
52 reader.readEntries(win, fail);
61 // downloads HTML file from server, saves it in cache and calls success with file contents
62 this.withHtmlFromServer = function(book_id, success, error) {
63 debug('info:withHtmlFromServer: id:' + book_id);
65 Catalogue.withBook(book_id, function(book) {
66 var url = WL + book.html_file;
67 debug('info:withHtmlFromServer: fetching url: ' + url);
69 View.spinner("Pobieranie treści utworu z sieci");
71 var xhr = new XMLHttpRequest();
73 xhr.onload = function() {
74 data = xhr.responseText;
75 if(xhr.status != 200) {
77 error && error('Błąd podczas pobierania pliku');
80 self.root.getDirectory('html', {create: true}, function(dir) {
81 var reader = dir.getFile(''+book_id, {create: true}, function(f) {
82 f.createWriter(function(w) {
84 self.db.transaction(function(tx) {
86 tx.executeSql("UPDATE book SET _local=1 WHERE id="+book_id);
93 success && success(data);
96 xhr.onerror = function(e) {
97 debug('Błąd podczas pobierania pliku')
98 error && error("error: " + data);
104 // calls the callback with contents of the HTML file for a given book,
105 // loaded from the server and cached locally
106 this.withHtml = function(id, success, error) {
107 debug('info:withHtml: id:' + id);
108 self.withLocalHtml(id, success, function() {
109 self.withHtmlFromServer(id, success, error);
114 this.clear = function() {
115 FileRepo.withLocal(function(local) {
123 this.deleteIfExists = function(id) {
125 self.root.getFile('html/' + id, {create: false}, function(f) {