cronapp-cordova-plugin-contentsync 4.4.0-RC7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +11 -0
- package/README.md +372 -0
- package/package-lock.json +1545 -0
- package/package.json +58 -0
- package/plugin.xml +93 -0
- package/sample/css/index.css +128 -0
- package/sample/img/logo.png +0 -0
- package/sample/index.html +47 -0
- package/sample/js/index.js +152 -0
- package/spec/helper/cordova.js +83 -0
- package/spec/index.spec.js +334 -0
- package/src/android/Sync.java +1102 -0
- package/src/browser/Sync.js +20 -0
- package/src/ios/ContentSync.h +74 -0
- package/src/ios/ContentSync.m +1002 -0
- package/src/ios/SSZipArchive.h +52 -0
- package/src/ios/SSZipArchive.m +540 -0
- package/src/ios/minizip/crypt.h +131 -0
- package/src/ios/minizip/ioapi.c +239 -0
- package/src/ios/minizip/ioapi.h +201 -0
- package/src/ios/minizip/mztools.c +284 -0
- package/src/ios/minizip/mztools.h +31 -0
- package/src/ios/minizip/unzip.c +2153 -0
- package/src/ios/minizip/unzip.h +437 -0
- package/src/ios/minizip/zip.c +2022 -0
- package/src/ios/minizip/zip.h +362 -0
- package/src/windows/SyncProxy.js +279 -0
- package/src/windows/ZipWinProj/PGZipInflate.cs +94 -0
- package/src/windows/ZipWinProj/Properties/AssemblyInfo.cs +30 -0
- package/src/windows/ZipWinProj/ZipWinProj.csproj +57 -0
- package/src/wp8/Sync.cs +746 -0
- package/src/wp8/Unzip.cs +481 -0
- package/tests/anyfile.txt +1 -0
- package/tests/archives/www1.zip +0 -0
- package/tests/archives/www2.zip +0 -0
- package/tests/package.json +11 -0
- package/tests/plugin.xml +22 -0
- package/tests/scripts/start-server.sh +2 -0
- package/tests/scripts/stop-server.sh +1 -0
- package/tests/tests.js +255 -0
- package/www/index.js +285 -0
package/src/wp8/Sync.cs
ADDED
|
@@ -0,0 +1,746 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
you may not use this file except in compliance with the License.
|
|
4
|
+
You may obtain a copy of the License at
|
|
5
|
+
|
|
6
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
|
|
8
|
+
Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
See the License for the specific language governing permissions and
|
|
12
|
+
limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
using System;
|
|
16
|
+
using System.Collections.Generic;
|
|
17
|
+
using System.IO;
|
|
18
|
+
using System.IO.IsolatedStorage;
|
|
19
|
+
using System.Net;
|
|
20
|
+
using System.Runtime.Serialization;
|
|
21
|
+
using System.Windows;
|
|
22
|
+
using System.Security;
|
|
23
|
+
using System.Diagnostics;
|
|
24
|
+
|
|
25
|
+
namespace WPCordovaClassLib.Cordova.Commands
|
|
26
|
+
{
|
|
27
|
+
public class Sync : BaseCommand
|
|
28
|
+
{
|
|
29
|
+
public class DownloadRequestState
|
|
30
|
+
{
|
|
31
|
+
// This class stores the State of the request.
|
|
32
|
+
public HttpWebRequest request;
|
|
33
|
+
public TransferOptions options;
|
|
34
|
+
public bool isCancelled;
|
|
35
|
+
|
|
36
|
+
public DownloadRequestState()
|
|
37
|
+
{
|
|
38
|
+
request = null;
|
|
39
|
+
options = null;
|
|
40
|
+
isCancelled = false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public class TransferOptions
|
|
45
|
+
{
|
|
46
|
+
/// File path to upload OR File path to download to
|
|
47
|
+
public string FilePath { get; set; }
|
|
48
|
+
|
|
49
|
+
public string Url { get; set; }
|
|
50
|
+
/// Flag to recognize if we should trust every host (only in debug environments)
|
|
51
|
+
public bool TrustAllHosts { get; set; }
|
|
52
|
+
public string Id { get; set; }
|
|
53
|
+
public string Headers { get; set; }
|
|
54
|
+
public string CallbackId { get; set; }
|
|
55
|
+
public bool ChunkedMode { get; set; }
|
|
56
|
+
public int Type { get; set; }
|
|
57
|
+
public bool CopyCordovaAssets { get; set; }
|
|
58
|
+
public bool CopyRootApp { get; set; }
|
|
59
|
+
public int Timeout { get; set; }
|
|
60
|
+
public string Manifest { get; set; }
|
|
61
|
+
/// Server address
|
|
62
|
+
public string Server { get; set; }
|
|
63
|
+
/// File key
|
|
64
|
+
public string FileKey { get; set; }
|
|
65
|
+
/// File name on the server
|
|
66
|
+
public string FileName { get; set; }
|
|
67
|
+
/// File Mime type
|
|
68
|
+
public string MimeType { get; set; }
|
|
69
|
+
/// Additional options
|
|
70
|
+
public string Params { get; set; }
|
|
71
|
+
public string Method { get; set; }
|
|
72
|
+
|
|
73
|
+
public TransferOptions()
|
|
74
|
+
{
|
|
75
|
+
FileKey = "file";
|
|
76
|
+
FileName = "image.jpg";
|
|
77
|
+
MimeType = "image/jpeg";
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// <summary>
|
|
82
|
+
/// Boundary symbol
|
|
83
|
+
/// </summary>
|
|
84
|
+
private string Boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
|
|
85
|
+
|
|
86
|
+
// Error codes
|
|
87
|
+
public const int InvalidUrlError = 1;
|
|
88
|
+
public const int ConnectionError = 2;
|
|
89
|
+
public const int UnzipError = 3;
|
|
90
|
+
public const int AbortError = 4; // not really an error, but whatevs
|
|
91
|
+
|
|
92
|
+
// Sync strategy codes
|
|
93
|
+
public const int Replace = 1;
|
|
94
|
+
public const int Merge = 2;
|
|
95
|
+
|
|
96
|
+
private static Dictionary<string, DownloadRequestState> InProcDownloads = new Dictionary<string,DownloadRequestState>();
|
|
97
|
+
|
|
98
|
+
/// <summary>
|
|
99
|
+
/// Represents transfer error codes for callback
|
|
100
|
+
/// </summary>
|
|
101
|
+
[DataContract]
|
|
102
|
+
public class SyncError
|
|
103
|
+
{
|
|
104
|
+
/// <summary>
|
|
105
|
+
/// Error code
|
|
106
|
+
/// </summary>
|
|
107
|
+
[DataMember(Name = "code", IsRequired = true)]
|
|
108
|
+
public int Code { get; set; }
|
|
109
|
+
|
|
110
|
+
/// <summary>
|
|
111
|
+
/// The source URI
|
|
112
|
+
/// </summary>
|
|
113
|
+
[DataMember(Name = "source", IsRequired = true)]
|
|
114
|
+
public string Source { get; set; }
|
|
115
|
+
|
|
116
|
+
/// <summary>
|
|
117
|
+
/// The target URI
|
|
118
|
+
/// </summary>
|
|
119
|
+
///
|
|
120
|
+
[DataMember(Name = "target", IsRequired = true)]
|
|
121
|
+
public string Target { get; set; }
|
|
122
|
+
|
|
123
|
+
[DataMember(Name = "body", IsRequired = true)]
|
|
124
|
+
public string Body { get; set; }
|
|
125
|
+
|
|
126
|
+
/// <summary>
|
|
127
|
+
/// The http status code response from the remote URI
|
|
128
|
+
/// </summary>
|
|
129
|
+
[DataMember(Name = "http_status", IsRequired = true)]
|
|
130
|
+
public int HttpStatus { get; set; }
|
|
131
|
+
|
|
132
|
+
/// <summary>
|
|
133
|
+
/// Creates SyncError object
|
|
134
|
+
/// </summary>
|
|
135
|
+
/// <param name="errorCode">Error code</param>
|
|
136
|
+
public SyncError(int errorCode)
|
|
137
|
+
{
|
|
138
|
+
this.Code = errorCode;
|
|
139
|
+
this.Source = null;
|
|
140
|
+
this.Target = null;
|
|
141
|
+
this.HttpStatus = 0;
|
|
142
|
+
this.Body = "";
|
|
143
|
+
}
|
|
144
|
+
public SyncError(int errorCode, string source, string target, int status, string body = "")
|
|
145
|
+
{
|
|
146
|
+
this.Code = errorCode;
|
|
147
|
+
this.Source = source;
|
|
148
|
+
this.Target = target;
|
|
149
|
+
this.HttpStatus = status;
|
|
150
|
+
this.Body = body;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/// <summary>
|
|
155
|
+
/// Represents a singular progress event to be passed back to javascript
|
|
156
|
+
/// </summary>
|
|
157
|
+
[DataContract]
|
|
158
|
+
public class SyncProgress
|
|
159
|
+
{
|
|
160
|
+
/// <summary>
|
|
161
|
+
/// Is the length of the response known?
|
|
162
|
+
/// </summary>
|
|
163
|
+
[DataMember(Name = "lengthComputable", IsRequired = true)]
|
|
164
|
+
public bool LengthComputable { get; set; }
|
|
165
|
+
/// <summary>
|
|
166
|
+
/// amount of bytes loaded
|
|
167
|
+
/// </summary>
|
|
168
|
+
[DataMember(Name = "loaded", IsRequired = true)]
|
|
169
|
+
public long BytesLoaded { get; set; }
|
|
170
|
+
/// <summary>
|
|
171
|
+
/// Total bytes
|
|
172
|
+
/// </summary>
|
|
173
|
+
[DataMember(Name = "total", IsRequired = false)]
|
|
174
|
+
public long BytesTotal { get; set; }
|
|
175
|
+
|
|
176
|
+
public SyncProgress(long bTotal = 0, long bLoaded = 0)
|
|
177
|
+
{
|
|
178
|
+
LengthComputable = bTotal > 0;
|
|
179
|
+
BytesLoaded = bLoaded;
|
|
180
|
+
BytesTotal = bTotal;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// example : "{\"Authorization\":\"Basic Y29yZG92YV91c2VyOmNvcmRvdmFfcGFzc3dvcmQ=\"}"
|
|
185
|
+
protected Dictionary<string,string> parseHeaders(string jsonHeaders)
|
|
186
|
+
{
|
|
187
|
+
try
|
|
188
|
+
{
|
|
189
|
+
Dictionary<string, string> result = new Dictionary<string, string>();
|
|
190
|
+
|
|
191
|
+
string temp = jsonHeaders.StartsWith("{") ? jsonHeaders.Substring(1) : jsonHeaders;
|
|
192
|
+
temp = temp.EndsWith("}") ? temp.Substring(0, temp.Length - 1) : temp;
|
|
193
|
+
|
|
194
|
+
string[] strHeaders = temp.Split(',');
|
|
195
|
+
for (int n = 0; n < strHeaders.Length; n++)
|
|
196
|
+
{
|
|
197
|
+
// we need to use indexOf in order to WP7 compatible
|
|
198
|
+
int splitIndex = strHeaders[n].IndexOf(':');
|
|
199
|
+
if (splitIndex > 0)
|
|
200
|
+
{
|
|
201
|
+
string[] split = new string[2];
|
|
202
|
+
split[0] = strHeaders[n].Substring(0, splitIndex);
|
|
203
|
+
split[1] = strHeaders[n].Substring(splitIndex + 1);
|
|
204
|
+
|
|
205
|
+
split[0] = JSON.JsonHelper.Deserialize<string>(split[0]);
|
|
206
|
+
split[1] = JSON.JsonHelper.Deserialize<string>(split[1]);
|
|
207
|
+
result[split[0]] = split[1];
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
catch (Exception)
|
|
213
|
+
{
|
|
214
|
+
Debug.WriteLine("Failed to parseHeaders from string :: " + jsonHeaders);
|
|
215
|
+
}
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
public void sync(string options)
|
|
220
|
+
{
|
|
221
|
+
TransferOptions downloadOptions = null;
|
|
222
|
+
HttpWebRequest webRequest = null;
|
|
223
|
+
string callbackId;
|
|
224
|
+
|
|
225
|
+
try
|
|
226
|
+
{
|
|
227
|
+
// options.src, options.type, options.headers, options.id
|
|
228
|
+
string[] optionStrings = JSON.JsonHelper.Deserialize<string[]>(options);
|
|
229
|
+
|
|
230
|
+
downloadOptions = new TransferOptions();
|
|
231
|
+
downloadOptions.Url = optionStrings[0];
|
|
232
|
+
|
|
233
|
+
downloadOptions.Id = optionStrings[1];
|
|
234
|
+
|
|
235
|
+
downloadOptions.FilePath = "content_sync/downloads/" + downloadOptions.Id;
|
|
236
|
+
|
|
237
|
+
if (String.Equals(optionStrings[2], "replace"))
|
|
238
|
+
{
|
|
239
|
+
downloadOptions.Type = Replace;
|
|
240
|
+
}
|
|
241
|
+
else
|
|
242
|
+
{
|
|
243
|
+
downloadOptions.Type = Merge;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
downloadOptions.Headers = optionStrings[3];
|
|
247
|
+
|
|
248
|
+
bool copyCordovaAssets = false;
|
|
249
|
+
bool.TryParse(optionStrings[4], out copyCordovaAssets);
|
|
250
|
+
downloadOptions.CopyCordovaAssets = copyCordovaAssets;
|
|
251
|
+
|
|
252
|
+
bool copyRootApp = false;
|
|
253
|
+
bool.TryParse(optionStrings[5], out copyRootApp);
|
|
254
|
+
downloadOptions.CopyRootApp = copyRootApp;
|
|
255
|
+
|
|
256
|
+
downloadOptions.Timeout = Convert.ToInt32(optionStrings[6]);
|
|
257
|
+
|
|
258
|
+
bool trustAll = false;
|
|
259
|
+
bool.TryParse(optionStrings[7], out trustAll);
|
|
260
|
+
downloadOptions.TrustAllHosts = trustAll;
|
|
261
|
+
|
|
262
|
+
downloadOptions.Manifest = optionStrings[8];
|
|
263
|
+
|
|
264
|
+
downloadOptions.CallbackId = callbackId = optionStrings[optionStrings.Length-1];
|
|
265
|
+
}
|
|
266
|
+
catch (Exception)
|
|
267
|
+
{
|
|
268
|
+
DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
try
|
|
273
|
+
{
|
|
274
|
+
// not sure if we still need this
|
|
275
|
+
// is the URL a local app file?
|
|
276
|
+
if (downloadOptions.Url.StartsWith("x-wmapp0") || downloadOptions.Url.StartsWith("file:"))
|
|
277
|
+
{
|
|
278
|
+
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
|
|
279
|
+
{
|
|
280
|
+
string cleanUrl = downloadOptions.Url.Replace("x-wmapp0:", "").Replace("file:", "").Replace("//","");
|
|
281
|
+
|
|
282
|
+
// pre-emptively create any directories in the FilePath that do not exist
|
|
283
|
+
string directoryName = getDirectoryName(downloadOptions.FilePath);
|
|
284
|
+
if (!string.IsNullOrEmpty(directoryName) && !isoFile.DirectoryExists(directoryName))
|
|
285
|
+
{
|
|
286
|
+
isoFile.CreateDirectory(directoryName);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// just copy from one area of iso-store to another ...
|
|
290
|
+
if (isoFile.FileExists(downloadOptions.Url))
|
|
291
|
+
{
|
|
292
|
+
isoFile.CopyFile(downloadOptions.Url, downloadOptions.FilePath);
|
|
293
|
+
}
|
|
294
|
+
else
|
|
295
|
+
{
|
|
296
|
+
// need to unpack resource from the dll
|
|
297
|
+
Uri uri = new Uri(cleanUrl, UriKind.Relative);
|
|
298
|
+
var resource = Application.GetResourceStream(uri);
|
|
299
|
+
|
|
300
|
+
if (resource != null)
|
|
301
|
+
{
|
|
302
|
+
// create the file destination
|
|
303
|
+
if (!isoFile.FileExists(downloadOptions.FilePath))
|
|
304
|
+
{
|
|
305
|
+
var destFile = isoFile.CreateFile(downloadOptions.FilePath);
|
|
306
|
+
destFile.Close();
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
using (FileStream fileStream = new IsolatedStorageFileStream(downloadOptions.FilePath, FileMode.Create, FileAccess.Write, isoFile))
|
|
310
|
+
{
|
|
311
|
+
long totalBytes = resource.Stream.Length;
|
|
312
|
+
int bytesRead = 0;
|
|
313
|
+
using (BinaryReader reader = new BinaryReader(resource.Stream))
|
|
314
|
+
{
|
|
315
|
+
using (BinaryWriter writer = new BinaryWriter(fileStream))
|
|
316
|
+
{
|
|
317
|
+
int BUFFER_SIZE = 1024;
|
|
318
|
+
byte[] buffer;
|
|
319
|
+
|
|
320
|
+
while (true)
|
|
321
|
+
{
|
|
322
|
+
buffer = reader.ReadBytes(BUFFER_SIZE);
|
|
323
|
+
// fire a progress event ?
|
|
324
|
+
bytesRead += buffer.Length;
|
|
325
|
+
if (buffer.Length > 0)
|
|
326
|
+
{
|
|
327
|
+
writer.Write(buffer);
|
|
328
|
+
DispatchSyncProgress(bytesRead, totalBytes, 1, callbackId);
|
|
329
|
+
}
|
|
330
|
+
else
|
|
331
|
+
{
|
|
332
|
+
writer.Close();
|
|
333
|
+
reader.Close();
|
|
334
|
+
fileStream.Close();
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
string result = "{ \"localPath\": \"" + downloadOptions.FilePath + "\" , \"Id\" : \"" + downloadOptions.Id + "\"}";
|
|
346
|
+
if (result != null)
|
|
347
|
+
{
|
|
348
|
+
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, result), callbackId);
|
|
349
|
+
}
|
|
350
|
+
else
|
|
351
|
+
{
|
|
352
|
+
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, 0), callbackId);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
else
|
|
358
|
+
{
|
|
359
|
+
// otherwise it is web-bound, we will actually download it
|
|
360
|
+
//Debug.WriteLine("Creating WebRequest for url : " + downloadOptions.Url);
|
|
361
|
+
webRequest = (HttpWebRequest)WebRequest.Create(downloadOptions.Url);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
catch (Exception /*ex*/)
|
|
365
|
+
{
|
|
366
|
+
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,
|
|
367
|
+
new SyncError(InvalidUrlError, downloadOptions.Url, null, 0)));
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (downloadOptions != null && webRequest != null)
|
|
372
|
+
{
|
|
373
|
+
DownloadRequestState state = new DownloadRequestState();
|
|
374
|
+
state.options = downloadOptions;
|
|
375
|
+
state.request = webRequest;
|
|
376
|
+
InProcDownloads[downloadOptions.Id] = state;
|
|
377
|
+
|
|
378
|
+
if (!string.IsNullOrEmpty(downloadOptions.Headers))
|
|
379
|
+
{
|
|
380
|
+
Dictionary<string, string> headers = parseHeaders(downloadOptions.Headers);
|
|
381
|
+
foreach (string key in headers.Keys)
|
|
382
|
+
{
|
|
383
|
+
webRequest.Headers[key] = headers[key];
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
try
|
|
388
|
+
{
|
|
389
|
+
webRequest.BeginGetResponse(new AsyncCallback(downloadCallback), state);
|
|
390
|
+
}
|
|
391
|
+
catch (WebException)
|
|
392
|
+
{
|
|
393
|
+
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,
|
|
394
|
+
new SyncError(InvalidUrlError, downloadOptions.Url, null, 0)));
|
|
395
|
+
}
|
|
396
|
+
// dispatch an event for progress ( 0 )
|
|
397
|
+
lock (state)
|
|
398
|
+
{
|
|
399
|
+
if (!state.isCancelled)
|
|
400
|
+
{
|
|
401
|
+
var plugRes = new PluginResult(PluginResult.Status.OK, new SyncProgress());
|
|
402
|
+
plugRes.KeepCallback = true;
|
|
403
|
+
plugRes.CallbackId = callbackId;
|
|
404
|
+
DispatchCommandResult(plugRes, callbackId);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
public void cancel(string options)
|
|
411
|
+
{
|
|
412
|
+
Debug.WriteLine("cancel :: " + options);
|
|
413
|
+
string[] optionStrings = JSON.JsonHelper.Deserialize<string[]>(options);
|
|
414
|
+
string id = optionStrings[0];
|
|
415
|
+
string callbackId = optionStrings[1];
|
|
416
|
+
|
|
417
|
+
if (InProcDownloads.ContainsKey(id))
|
|
418
|
+
{
|
|
419
|
+
DownloadRequestState state = InProcDownloads[id];
|
|
420
|
+
if (!state.isCancelled)
|
|
421
|
+
{ // prevent multiple callbacks for the same cancel
|
|
422
|
+
state.isCancelled = true;
|
|
423
|
+
if (!state.request.HaveResponse)
|
|
424
|
+
{
|
|
425
|
+
state.request.Abort();
|
|
426
|
+
InProcDownloads.Remove(id);
|
|
427
|
+
//callbackId = state.options.CallbackId;
|
|
428
|
+
//state = null;
|
|
429
|
+
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,
|
|
430
|
+
new SyncError(Sync.AbortError)),
|
|
431
|
+
state.options.CallbackId);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
else
|
|
436
|
+
{
|
|
437
|
+
DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION), callbackId); // TODO: is it an IO exception?
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
private void DispatchSyncProgress(long bytesLoaded, long bytesTotal, int status, string callbackId, bool keepCallback = true)
|
|
442
|
+
{
|
|
443
|
+
//Debug.WriteLine("DispatchSyncProgress : " + callbackId);
|
|
444
|
+
// send a progress change event
|
|
445
|
+
SyncProgress progEvent = new SyncProgress(bytesTotal);
|
|
446
|
+
progEvent.BytesLoaded = bytesLoaded;
|
|
447
|
+
|
|
448
|
+
int percent = (int)((bytesLoaded / (double)bytesTotal) * 100);
|
|
449
|
+
|
|
450
|
+
// jump from 50 to 100 once unzip is done
|
|
451
|
+
if(bytesLoaded != bytesTotal && status != 3){
|
|
452
|
+
percent = percent / 2;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
string result = "{\"progress\":" + percent + ", \"status\":" + status + "}";
|
|
456
|
+
|
|
457
|
+
PluginResult plugRes = new PluginResult(PluginResult.Status.OK, result);
|
|
458
|
+
plugRes.KeepCallback = keepCallback;
|
|
459
|
+
plugRes.CallbackId = callbackId;
|
|
460
|
+
DispatchCommandResult(plugRes, callbackId);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/// <summary>
|
|
464
|
+
///
|
|
465
|
+
/// </summary>
|
|
466
|
+
/// <param name="asynchronousResult"></param>
|
|
467
|
+
private void downloadCallback(IAsyncResult asynchronousResult)
|
|
468
|
+
{
|
|
469
|
+
DownloadRequestState reqState = (DownloadRequestState)asynchronousResult.AsyncState;
|
|
470
|
+
HttpWebRequest request = reqState.request;
|
|
471
|
+
|
|
472
|
+
string callbackId = reqState.options.CallbackId;
|
|
473
|
+
try
|
|
474
|
+
{
|
|
475
|
+
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
|
|
476
|
+
|
|
477
|
+
// send a progress change event
|
|
478
|
+
DispatchSyncProgress(0, response.ContentLength, 0, callbackId);
|
|
479
|
+
|
|
480
|
+
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
|
|
481
|
+
{
|
|
482
|
+
// create any directories in the path that do not exist
|
|
483
|
+
string directoryName = getDirectoryName(reqState.options.FilePath);
|
|
484
|
+
if (!string.IsNullOrEmpty(directoryName) && !isoFile.DirectoryExists(directoryName))
|
|
485
|
+
{
|
|
486
|
+
isoFile.CreateDirectory(directoryName);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// make sure we delete the file if it exists
|
|
490
|
+
if(isoFile.FileExists(reqState.options.FilePath))
|
|
491
|
+
{
|
|
492
|
+
isoFile.DeleteFile(reqState.options.FilePath);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
if (!isoFile.FileExists(reqState.options.FilePath))
|
|
496
|
+
{
|
|
497
|
+
var file = isoFile.CreateFile(reqState.options.FilePath);
|
|
498
|
+
file.Close();
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
using (FileStream fileStream = new IsolatedStorageFileStream(reqState.options.FilePath, FileMode.Open, FileAccess.Write, isoFile))
|
|
502
|
+
{
|
|
503
|
+
long totalBytes = response.ContentLength;
|
|
504
|
+
int bytesRead = 0;
|
|
505
|
+
using (BinaryReader reader = new BinaryReader(response.GetResponseStream()))
|
|
506
|
+
{
|
|
507
|
+
using (BinaryWriter writer = new BinaryWriter(fileStream))
|
|
508
|
+
{
|
|
509
|
+
int BUFFER_SIZE = 1024;
|
|
510
|
+
byte[] buffer;
|
|
511
|
+
|
|
512
|
+
while (true)
|
|
513
|
+
{
|
|
514
|
+
buffer = reader.ReadBytes(BUFFER_SIZE);
|
|
515
|
+
// fire a progress event ?
|
|
516
|
+
bytesRead += buffer.Length;
|
|
517
|
+
if (buffer.Length > 0 && !reqState.isCancelled)
|
|
518
|
+
{
|
|
519
|
+
writer.Write(buffer);
|
|
520
|
+
DispatchSyncProgress(bytesRead, totalBytes, 1, callbackId);
|
|
521
|
+
}
|
|
522
|
+
else
|
|
523
|
+
{
|
|
524
|
+
writer.Close();
|
|
525
|
+
reader.Close();
|
|
526
|
+
fileStream.Close();
|
|
527
|
+
break;
|
|
528
|
+
}
|
|
529
|
+
System.Threading.Thread.Sleep(1);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
if (reqState.isCancelled)
|
|
535
|
+
{
|
|
536
|
+
isoFile.DeleteFile(reqState.options.FilePath);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
if (reqState.isCancelled)
|
|
541
|
+
{
|
|
542
|
+
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new SyncError(AbortError)),
|
|
543
|
+
callbackId);
|
|
544
|
+
}
|
|
545
|
+
else
|
|
546
|
+
{
|
|
547
|
+
UnZip unzipper = new UnZip();
|
|
548
|
+
string destFilePath = "www/" + reqState.options.FilePath;
|
|
549
|
+
// at this point, bytesLoaded = bytesTotal so we'll just put the as '1'
|
|
550
|
+
DispatchSyncProgress(1, 1, 2, callbackId);
|
|
551
|
+
unzipper.unzip(reqState.options.FilePath, destFilePath, reqState.options.Type);
|
|
552
|
+
|
|
553
|
+
if(reqState.options.CopyCordovaAssets)
|
|
554
|
+
{
|
|
555
|
+
copyCordovaAssets(destFilePath);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
DispatchSyncProgress(1, 1, 3, callbackId);
|
|
559
|
+
string result = "{ \"localPath\": \"" + reqState.options.FilePath + "\" , \"Id\" : \"" + reqState.options.Id + "\"}";
|
|
560
|
+
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, result), callbackId);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
catch (IsolatedStorageException)
|
|
564
|
+
{
|
|
565
|
+
// Trying to write the file somewhere within the IsoStorage.
|
|
566
|
+
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new SyncError(UnzipError)),
|
|
567
|
+
callbackId);
|
|
568
|
+
}
|
|
569
|
+
catch (SecurityException)
|
|
570
|
+
{
|
|
571
|
+
// Trying to write the file somewhere not allowed.
|
|
572
|
+
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new SyncError(UnzipError)),
|
|
573
|
+
callbackId);
|
|
574
|
+
}
|
|
575
|
+
catch (WebException webex)
|
|
576
|
+
{
|
|
577
|
+
// TODO: probably need better work here to properly respond with all http status codes back to JS
|
|
578
|
+
// Right now am jumping through hoops just to detect 404.
|
|
579
|
+
HttpWebResponse response = (HttpWebResponse)webex.Response;
|
|
580
|
+
if ((webex.Status == WebExceptionStatus.ProtocolError && response.StatusCode == HttpStatusCode.NotFound)
|
|
581
|
+
|| webex.Status == WebExceptionStatus.UnknownError)
|
|
582
|
+
{
|
|
583
|
+
// Weird MSFT detection of 404... seriously... just give us the f(*&#$@ status code as a number ffs!!!
|
|
584
|
+
// "Numbers for HTTP status codes? Nah.... let's create our own set of enums/structs to abstract that stuff away."
|
|
585
|
+
// FACEPALM
|
|
586
|
+
// Or just cast it to an int, whiner ... -jm
|
|
587
|
+
int statusCode = (int)response.StatusCode;
|
|
588
|
+
string body = "";
|
|
589
|
+
|
|
590
|
+
using (Stream streamResponse = response.GetResponseStream())
|
|
591
|
+
{
|
|
592
|
+
using (StreamReader streamReader = new StreamReader(streamResponse))
|
|
593
|
+
{
|
|
594
|
+
body = streamReader.ReadToEnd();
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
SyncError ftError = new SyncError(ConnectionError, null, null, statusCode, body);
|
|
598
|
+
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ftError),
|
|
599
|
+
callbackId);
|
|
600
|
+
}
|
|
601
|
+
else
|
|
602
|
+
{
|
|
603
|
+
lock (reqState)
|
|
604
|
+
{
|
|
605
|
+
if (!reqState.isCancelled)
|
|
606
|
+
{
|
|
607
|
+
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,
|
|
608
|
+
new SyncError(ConnectionError)),
|
|
609
|
+
callbackId);
|
|
610
|
+
}
|
|
611
|
+
else
|
|
612
|
+
{
|
|
613
|
+
Debug.WriteLine("It happened");
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
catch (Exception)
|
|
619
|
+
{
|
|
620
|
+
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,
|
|
621
|
+
new SyncError(UnzipError)),
|
|
622
|
+
callbackId);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
//System.Threading.Thread.Sleep(1000);
|
|
626
|
+
if (InProcDownloads.ContainsKey(reqState.options.Id))
|
|
627
|
+
{
|
|
628
|
+
InProcDownloads.Remove(reqState.options.Id);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// Gets the full path without the filename
|
|
633
|
+
private string getDirectoryName(String filePath)
|
|
634
|
+
{
|
|
635
|
+
string directoryName;
|
|
636
|
+
try
|
|
637
|
+
{
|
|
638
|
+
directoryName = filePath.Substring(0, filePath.LastIndexOf('/'));
|
|
639
|
+
}
|
|
640
|
+
catch
|
|
641
|
+
{
|
|
642
|
+
directoryName = "";
|
|
643
|
+
}
|
|
644
|
+
return directoryName;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
private void copyCordovaAssets(string destFilePath)
|
|
648
|
+
{
|
|
649
|
+
copyCordovaPlugins("x-wmapp0:www/cordova.js", destFilePath + "/www/cordova.js");
|
|
650
|
+
copyCordovaPlugins("x-wmapp0:www/cordova_plugins.js", destFilePath + "/www/cordova_plugins.js");
|
|
651
|
+
|
|
652
|
+
Uri uri = new Uri("x-wmapp0:www/cordova_plugins.js", UriKind.RelativeOrAbsolute);
|
|
653
|
+
Uri relUri = new Uri(uri.AbsolutePath, UriKind.Relative);
|
|
654
|
+
var resource = Application.GetResourceStream(relUri);
|
|
655
|
+
|
|
656
|
+
using (StreamReader streamReader = new StreamReader(resource.Stream))
|
|
657
|
+
{
|
|
658
|
+
// have to parse cordova_plugins.js to find all the file paths - kinda messy
|
|
659
|
+
string cordovaPluginsText = streamReader.ReadToEnd();
|
|
660
|
+
string parsedCordovaPlugins = cordovaPluginsText;
|
|
661
|
+
string[] result;
|
|
662
|
+
string[] jsonSepOne = new string[] { "module.exports=" };
|
|
663
|
+
string[] jsonSepTwo = new string[] { ";module.exports.metadata" };
|
|
664
|
+
|
|
665
|
+
parsedCordovaPlugins = parsedCordovaPlugins.Replace(" ", "");
|
|
666
|
+
parsedCordovaPlugins = parsedCordovaPlugins.Replace("\n", "");
|
|
667
|
+
|
|
668
|
+
result = parsedCordovaPlugins.Split(jsonSepOne, StringSplitOptions.RemoveEmptyEntries);
|
|
669
|
+
result = result[1].Split(jsonSepTwo, StringSplitOptions.RemoveEmptyEntries);
|
|
670
|
+
cordova_plugin[] pluginsJSON = JSON.JsonHelper.Deserialize<cordova_plugin[]>(result[0]);
|
|
671
|
+
|
|
672
|
+
streamReader.Close();
|
|
673
|
+
for(var i=0;i<pluginsJSON.Length;i++)
|
|
674
|
+
{
|
|
675
|
+
//Debug.WriteLine("x-wmapp0:www/" + pluginsJSON[i].file + " to " + destFilePath + "/" + pluginsJSON[i].file);
|
|
676
|
+
copyCordovaPlugins("x-wmapp0:www/" + pluginsJSON[i].file, destFilePath + "/www/" + pluginsJSON[i].file);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
private void copyCordovaPlugins(string srcURL, string destURL)
|
|
682
|
+
{
|
|
683
|
+
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
|
|
684
|
+
{
|
|
685
|
+
string directoryName = getDirectoryName(destURL);
|
|
686
|
+
if (!string.IsNullOrEmpty(directoryName) && !isoFile.DirectoryExists(directoryName))
|
|
687
|
+
{
|
|
688
|
+
isoFile.CreateDirectory(directoryName);
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
Uri uri = new Uri(srcURL, UriKind.RelativeOrAbsolute);
|
|
692
|
+
Uri relUri = new Uri(uri.AbsolutePath, UriKind.Relative);
|
|
693
|
+
var resource = Application.GetResourceStream(relUri);
|
|
694
|
+
|
|
695
|
+
if (resource != null)
|
|
696
|
+
{
|
|
697
|
+
// create the file destination
|
|
698
|
+
if (!isoFile.FileExists(destURL))
|
|
699
|
+
{
|
|
700
|
+
var destFile = isoFile.CreateFile(destURL);
|
|
701
|
+
destFile.Close();
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
using (FileStream fileStream = new IsolatedStorageFileStream(destURL, FileMode.Create, FileAccess.Write, isoFile))
|
|
705
|
+
{
|
|
706
|
+
long totalBytes = resource.Stream.Length;
|
|
707
|
+
int bytesRead = 0;
|
|
708
|
+
using (BinaryReader reader = new BinaryReader(resource.Stream))
|
|
709
|
+
{
|
|
710
|
+
using (BinaryWriter writer = new BinaryWriter(fileStream))
|
|
711
|
+
{
|
|
712
|
+
int BUFFER_SIZE = 1024;
|
|
713
|
+
byte[] buffer;
|
|
714
|
+
|
|
715
|
+
//Debug.WriteLine("Copying url : " + srcURL + " to : " + destURL);
|
|
716
|
+
while (true)
|
|
717
|
+
{
|
|
718
|
+
buffer = reader.ReadBytes(BUFFER_SIZE);
|
|
719
|
+
// fire a progress event ?
|
|
720
|
+
bytesRead += buffer.Length;
|
|
721
|
+
if (buffer.Length > 0)
|
|
722
|
+
{
|
|
723
|
+
writer.Write(buffer);
|
|
724
|
+
}
|
|
725
|
+
else
|
|
726
|
+
{
|
|
727
|
+
writer.Close();
|
|
728
|
+
reader.Close();
|
|
729
|
+
fileStream.Close();
|
|
730
|
+
break;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
public class cordova_plugin
|
|
741
|
+
{
|
|
742
|
+
public string file;
|
|
743
|
+
public string id;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
}
|