copying info
[wl-mobile.git] / src / pl / org / nowoczesnapolska / wlmobi / DBPut.java
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 package pl.org.nowoczesnapolska.wlmobi;
7
8 import org.json.JSONArray;
9 import org.json.JSONException;
10
11 import android.util.Log;
12 import android.content.res.AssetManager;
13
14 import com.phonegap.api.Plugin;
15 import com.phonegap.api.PluginResult;
16
17 import java.io.File;
18 import java.io.FileOutputStream;
19 import java.io.InputStream;
20 import java.io.IOException;
21 import pl.org.nowoczesnapolska.wlmobi.Downloader;
22
23 public class DBPut extends Plugin{
24
25         @Override
26         public PluginResult execute(String action, JSONArray args, String callbackId) {
27                 if (action.equals("put")) {
28                         try {
29                                 return this.put(args.getString(0), args.getString(1), args.getString(2));
30                         } catch (JSONException e) {
31                                 return new PluginResult(PluginResult.Status.ERROR, "Param errrors");
32                         }
33                 }
34                 else if (action.equals("fetch")) {
35                         try {
36                                 return this.fetch(args.getString(0));
37                         } catch (JSONException e) {
38                                 return new PluginResult(PluginResult.Status.ERROR, "Param errrors");
39                         }
40                 }
41                 else {
42                         return new PluginResult(PluginResult.Status.INVALID_ACTION);
43                 }
44         }
45
46         private PluginResult fetch(String url) {
47                 String fileName = "0000000000000001.db";
48                 String targetPath = "/data/data/" + this.ctx.getPackageName() + "/app_database/file__0/";
49                 
50                 Log.d("DBPut", "database path: " + targetPath + " / " + fileName);
51                 
52                 Downloader d = new Downloader();
53                 return d.downloadUrl(url, targetPath, fileName, "true");
54         }
55
56
57         private PluginResult put(String assetPath, String targetPath, String overwrite) {
58                 // this hard-coding is kinda creepy, should probably create the db and use getDatabasePath instead
59                 String absoluteTargetPath = "/data/data/" + this.ctx.getPackageName() + "/app_database/" + targetPath;
60                 int index = absoluteTargetPath.lastIndexOf('/');
61                 String targetDir = absoluteTargetPath.substring(0, index);
62
63                 try {
64                         File dir = new File(targetDir);
65                         if(!dir.exists()) {
66                                 Log.d("DBPut", "directory " + targetDir + " created");
67                                 dir.mkdirs();
68                         }
69
70                         File fout = new File(absoluteTargetPath);
71
72                         if(overwrite.equals("false") && fout.exists()) {
73                                 Log.d("DBPut", "File already exists");
74                                 return new PluginResult(PluginResult.Status.OK, "exist");
75                         }
76
77                         FileOutputStream fos = new FileOutputStream(fout);
78
79                         AssetManager assetManager = this.ctx.getResources().getAssets();
80                         InputStream is = assetManager.open(assetPath);
81
82                         byte[] buffer = new byte[1024];
83                         int len1 = 0;
84
85                         while ( (len1 = is.read(buffer)) > 0 ) {
86                                 fos.write(buffer,0, len1);
87                         }
88
89                         fos.close();
90
91                         Log.d("DBPut", "Copied to " + absoluteTargetPath);
92                 } catch (IOException e) {
93                         Log.d("DBPut", "Error: " + e);
94                         return new PluginResult(PluginResult.Status.ERROR, "Error: " + e);
95                 }
96                 return new PluginResult(PluginResult.Status.OK, absoluteTargetPath);
97         }
98 }