Merge pull request #1429 from NativeScript/http-file-download

file download implemented
This commit is contained in:
Vladimir Enchev
2016-01-26 16:11:26 +02:00
5 changed files with 183 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import types = require("utils/types");
import * as utilsModule from "utils/utils";
import * as imageSourceModule from "image-source";
import * as platformModule from "platform";
import * as fsModule from "file-system";
// this is imported for definition purposes only
import http = require("http");
@@ -74,6 +75,28 @@ function onRequestComplete(requestId: number, result: com.tns.Async.Http.Request
rejectImage(new Error("Response content may not be converted to an Image"));
}
});
},
toFile: (destinationFilePath?: string) => {
var fs: typeof fsModule = require("file-system");
var fileName = callbacks.url;
if (!destinationFilePath) {
destinationFilePath = fs.path.join(fs.knownFolders.documents().path, fileName.substring(fileName.lastIndexOf('/') + 1));
}
var stream: java.io.FileOutputStream;
try {
var javaFile = new java.io.File(destinationFilePath);
stream = new java.io.FileOutputStream(javaFile);
stream.write(result.raw.toByteArray());
return fs.File.fromPath(destinationFilePath);
}
catch (exception) {
throw new Error(`Cannot save file with path: ${destinationFilePath}.`);
}
finally {
if (stream) {
stream.close();
}
}
}
},
statusCode: result.statusCode,
@@ -134,6 +157,7 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
// remember the callbacks so that we can use them when the CompleteCallback is called
var callbacks = {
url: options.url,
resolveCallback: resolve,
rejectCallback: reject
};

View File

@@ -5,6 +5,7 @@ import http = require("http");
import * as typesModule from "utils/types";
import * as imageSourceModule from "image-source";
import * as utilsModule from "utils/utils";
import * as fsModule from "file-system";
var GET = "GET";
var USER_AGENT_HEADER = "User-Agent";
@@ -92,6 +93,19 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
}
});
},
toFile: (destinationFilePath?: string) => {
var fs: typeof fsModule = require("file-system");
var fileName = options.url;
if (!destinationFilePath) {
destinationFilePath = fs.path.join(fs.knownFolders.documents().path, fileName.substring(fileName.lastIndexOf('/') + 1));
}
if (data instanceof NSData) {
data.writeToFileAtomically(destinationFilePath, true);
return fs.File.fromPath(destinationFilePath);
} else {
reject(new Error(`Cannot save file with path: ${destinationFilePath}.`));
}
}
},
statusCode: response.statusCode,

21
http/http.d.ts vendored
View File

@@ -3,7 +3,7 @@
*/
declare module "http" {
import image = require("image-source");
import fs = require("file-system");
/**
* Downloads the content from the specified URL as a string.
@@ -41,6 +41,20 @@ declare module "http" {
*/
export function getImage(options: HttpRequestOptions): Promise<image.ImageSource>
/**
* Downloads the content from the specified URL and attempts to save it as file.
* @param url The URL to request from.
* @param destinationFilePath Optional. The downloaded file path.
*/
export function getFile(url: string, destinationFilePath?: string): Promise<fs.File>
/**
* Downloads the content from the specified URL and attempts to save it as file.
* @param options An object that specifies various request options.
* @param destinationFilePath Optional. The downloaded file path.
*/
export function getFile(options: HttpRequestOptions, destinationFilePath?: string): Promise<fs.File>
/**
* Makes a generic http request using the provided options and returns a HttpResponse Object.
* @param options An object that specifies various request options.
@@ -120,5 +134,10 @@ declare module "http" {
* Gets the response body as ImageSource.
*/
toImage: () => Promise<image.ImageSource>;
/**
* Gets the response body as file.
*/
toFile: (destinationFilePath?: string) => fs.File;
}
}

View File

@@ -38,4 +38,18 @@ export function getImage(arg: any): Promise<image.ImageSource> {
r.content.toImage().then(source => resolve(source), e => reject(e));
}, e => reject(e));
});
}
export function getFile(arg: any, destinationFilePath?: string): Promise<any> {
return new Promise<any>((resolve, reject) => {
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
.then(r => {
try {
var file = r.content.toFile(destinationFilePath);
resolve(file);
} catch (e) {
reject(e);
}
}, e => reject(e));
});
}