|
1 | 1 | import * as types from 'tns-core-modules/utils/types';
|
2 |
| -import { Headers, HttpError, HttpRequestOptions, TNSHttpDebugging } from './http-request-common'; |
| 2 | +import { File, knownFolders, path } from 'tns-core-modules/file-system'; |
| 3 | +import { FileManager } from '../file/file'; |
| 4 | +import { Headers, HttpError, HttpRequestOptions, TNSHttpSettings, SaveImageStorageKey, isImageUrl, fileNameFromPath } from './http-request-common'; |
| 5 | +import { getString, setString } from 'tns-core-modules/application-settings'; |
3 | 6 |
|
4 | 7 | export type CancellablePromise = Promise<any> & { cancel: () => void };
|
5 | 8 |
|
@@ -302,6 +305,16 @@ class NSURLSessionTaskDelegateImpl extends NSObject
|
302 | 305 | } else {
|
303 | 306 | content = this._data;
|
304 | 307 | }
|
| 308 | + if (TNSHttpSettings.saveImage && TNSHttpSettings.currentlySavedImages && TNSHttpSettings.currentlySavedImages[this._url]) { |
| 309 | + // ensure saved to disk |
| 310 | + if (TNSHttpSettings.currentlySavedImages[this._url].localPath) { |
| 311 | + FileManager.writeFile(content, TNSHttpSettings.currentlySavedImages[this._url].localPath, function(error, result) { |
| 312 | + if (TNSHttpSettings.debug) { |
| 313 | + console.log('http image save:', error ? error : result); |
| 314 | + } |
| 315 | + }); |
| 316 | + } |
| 317 | + } |
305 | 318 |
|
306 | 319 | if (this._debuggerRequest) {
|
307 | 320 | this._debuggerRequest.mimeType = this._response.MIMEType;
|
@@ -377,14 +390,8 @@ export class Http {
|
377 | 390 |
|
378 | 391 | try {
|
379 | 392 |
|
380 |
| - let domainDebugger; |
381 |
| - let debugRequest; |
382 |
| - if (TNSHttpDebugging.enabled) { |
383 |
| - domainDebugger = require('tns-core-modules/debugger'); |
384 |
| - const network = domainDebugger.getNetwork(); |
385 |
| - debugRequest = network && network.create(); |
386 |
| - } |
387 |
| - |
| 393 | + const makeRemoteRequest = () => { |
| 394 | + // make remote request |
388 | 395 | const urlRequest = NSMutableURLRequest.requestWithURL(
|
389 | 396 | NSURL.URLWithString(options.url)
|
390 | 397 | );
|
@@ -455,6 +462,74 @@ export class Http {
|
455 | 462 | debugRequest.requestWillBeSent(request);
|
456 | 463 | }
|
457 | 464 | task.resume();
|
| 465 | + }; |
| 466 | + |
| 467 | + let domainDebugger; |
| 468 | + let debugRequest; |
| 469 | + if (TNSHttpSettings.debug) { |
| 470 | + domainDebugger = require('tns-core-modules/debugger'); |
| 471 | + const network = domainDebugger.getNetwork(); |
| 472 | + debugRequest = network && network.create(); |
| 473 | + } |
| 474 | + |
| 475 | + if (TNSHttpSettings.saveImage && isImageUrl(options.url)) { |
| 476 | + // handle saved images to disk |
| 477 | + if (!TNSHttpSettings.currentlySavedImages) { |
| 478 | + const stored = getString(SaveImageStorageKey); |
| 479 | + if (stored) { |
| 480 | + try { |
| 481 | + TNSHttpSettings.currentlySavedImages = JSON.parse(stored); |
| 482 | + } catch (err) { |
| 483 | + TNSHttpSettings.currentlySavedImages = {}; |
| 484 | + } |
| 485 | + } else { |
| 486 | + TNSHttpSettings.currentlySavedImages = {}; |
| 487 | + } |
| 488 | + } |
| 489 | + |
| 490 | + const imageSetting = TNSHttpSettings.currentlySavedImages[options.url]; |
| 491 | + const requests = imageSetting ? imageSetting.requests : 0; |
| 492 | + let localPath: string; |
| 493 | + if (imageSetting && imageSetting.localPath && File.exists(imageSetting.localPath)) { |
| 494 | + // previously saved to disk |
| 495 | + resolve({ |
| 496 | + url: options.url, |
| 497 | + responseText: '', |
| 498 | + statusCode: 200, |
| 499 | + content: File.fromPath(imageSetting.localPath).readSync(function(err) { |
| 500 | + if (TNSHttpSettings.debug) { |
| 501 | + console.log('http image load error:', err); |
| 502 | + } |
| 503 | + }), |
| 504 | + headers: { |
| 505 | + 'Content-Type': 'arraybuffer' |
| 506 | + } |
| 507 | + }); |
| 508 | + |
| 509 | + } else if (requests >= TNSHttpSettings.saveImage.numberOfRequests) { |
| 510 | + // setup to write to disk when response finishes |
| 511 | + let filename = fileNameFromPath(options.url); |
| 512 | + if (filename.indexOf('?')) { |
| 513 | + // strip any params if were any |
| 514 | + filename = filename.split('?')[0]; |
| 515 | + } |
| 516 | + localPath = path.join(knownFolders.documents().path, filename); |
| 517 | + makeRemoteRequest(); |
| 518 | + } |
| 519 | + |
| 520 | + // save settings |
| 521 | + TNSHttpSettings.currentlySavedImages[options.url] = { |
| 522 | + ...(imageSetting || {}), |
| 523 | + date: Date.now(), |
| 524 | + requests: requests + 1, |
| 525 | + localPath |
| 526 | + }; |
| 527 | + setString(SaveImageStorageKey, JSON.stringify(TNSHttpSettings.currentlySavedImages)); |
| 528 | + |
| 529 | + } else { |
| 530 | + makeRemoteRequest(); |
| 531 | + } |
| 532 | + |
458 | 533 | } catch (ex) {
|
459 | 534 | reject({
|
460 | 535 | type: HttpError.Error,
|
|
0 commit comments