db upload fix
[wl-mobile.git] / assets / www / js / filerepo.js
1 /*
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.
4  */
5
6 var FileRepo = new function() {
7         /* API for files repository */
8         var self = this;
9         this.root = null;
10
11         this.init = function(success, error) {
12                 self.initRoot(success);
13         };
14
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);
19                         self.root = fs.root;
20                         success && success();
21                 }, function() {
22                         console.log('local fs not found');
23                         success && success();
24                 });
25         };
26
27
28         this.withLocalHtml = function(book_id, success, error) {
29                 console.log('info:withLocalHtml: id:' + book_id);
30                 View.spinner('Otwieranie treści utworu');
31                 if (!self.root)
32                         error && error('info:withLocalHtml: no local html: no usable filesystem');
33
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);
41                 }
42                 xhr.onerror = error;
43                 xhr.send();
44         };
45
46
47         this.withLocal = function(win, fail) {
48                 if (self.root) {
49                         self.root.getDirectory('html', {}, function(dir) {
50                                 var reader = dir.createReader();
51                                 reader.readEntries(win, fail);
52                         });
53                 }
54                 else {
55                         win && win([]);
56                 }
57         }
58
59
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);
63                 // read file from WL
64                 Catalogue.withBook(book_id, function(book) {
65                         var url = WL + book.html_file;
66                         console.log('info:withHtmlFromServer: fetching url: ' + url);
67
68                         View.spinner("Pobieranie treści utworu z sieci");
69
70                         if (self.root) {
71                                 Downloader.downloadFile(url, self.root.fullPath + "/html/", ""+book_id, true,
72                                         function(data){
73                                                 console.log('info:withHtmlFromServer: loaded file from WL');
74                                                 self.withLocalHtml(book_id, success, error);
75                                         }, function(data) {
76                                                 console.log('error downloading file!')
77                                                 error && error("error: " + data);
78                                         });
79                         }
80                         else {
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();
84                                 xhr.open("GET", url);
85                                 xhr.onload = function() {
86                                         console.log('info:withHtmlFromServer: fetched by ajax: ' + url);
87                                         success && success(xhr.responseText);
88                                 }
89                                 xhr.onerror = function() {
90                                         console.log('error downloading file!')
91                                         error && error("error: " + data);
92                                 }
93                                 xhr.send();
94                         }
95                 });             
96         };
97         
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);
104                 });
105         };
106
107
108         this.clear = function() {
109                 FileRepo.withLocal(function(local) {
110                         for (i in local) {
111                                 local[i].remove();
112                         }
113                 });
114         };
115
116
117         this.deleteIfExists = function(id) {
118                 if (self.root) {
119                         self.root.getFile('html/' + id, {create: false}, function(f) {
120                                 f.remove();
121                         });
122                 }
123         }
124 };