From 7dac18b3192853a98a688ae2514bd6a284a0162b Mon Sep 17 00:00:00 2001 From: Shu Muto Date: Wed, 15 Nov 2017 18:10:31 +0900 Subject: [PATCH] Add text download service To add functionality for text download within client-side, this patch adds text-download.service. This service realize to download text file from text contents and file name within only browser. This function will be used for download key pair after its creation. Change-Id: Id079fa38c0ac0e79666fa7c5469afd58bf5228d3 --- .../util/file/text-download.service.js | 66 +++++++++++++++++++ .../util/file/text-download.service.spec.js | 34 ++++++++++ 2 files changed, 100 insertions(+) create mode 100644 horizon/static/framework/util/file/text-download.service.js create mode 100644 horizon/static/framework/util/file/text-download.service.spec.js diff --git a/horizon/static/framework/util/file/text-download.service.js b/horizon/static/framework/util/file/text-download.service.js new file mode 100644 index 0000000000..bba4789228 --- /dev/null +++ b/horizon/static/framework/util/file/text-download.service.js @@ -0,0 +1,66 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function() { + 'use strict'; + + angular + .module('horizon.framework.util.file') + .factory('horizon.framework.util.file.text-download', + textDownloadService); + + textDownloadService.$inject = ['$q', '$timeout']; + + /** + * @ngdoc service + * @name horizon.framework.util.file.textDownloadService + * @description + * Service for download text contetns as file. Used for client-side + * text file creation and download, such as download a private key + * file after key pair creation. + */ + function textDownloadService($q, $timeout) { + var service = { + downloadTextFile: downloadTextFile + }; + + return service; + + //////////////// + + function downloadTextFile(text, filename) { + // create text file as object url + var blob = new Blob([ text ], { "type" : "text/plain" }); + window.URL = window.URL || window.webkitURL; + var fileUrl = window.URL.createObjectURL(blob); + + // provide text as downloaded file + var a = angular.element(''); + a.attr("href", fileUrl); + a.attr("download", filename); + a.attr("target", "_blank"); + angular.element(document.body).append(a); + + var deferred = $q.defer(); + // start download after updating view + $timeout(startDownload, 0); + return deferred.promise; + + function startDownload() { + a[0].click(); + a.remove(); + deferred.resolve(filename); + } + } + } +})(); diff --git a/horizon/static/framework/util/file/text-download.service.spec.js b/horizon/static/framework/util/file/text-download.service.spec.js new file mode 100644 index 0000000000..796dc8bf89 --- /dev/null +++ b/horizon/static/framework/util/file/text-download.service.spec.js @@ -0,0 +1,34 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function() { + 'use strict'; + + describe('textDownloadService', function() { + var $scope, textDownload; + + beforeEach(module('horizon.framework.util.file')); + beforeEach(inject(function($injector) { + textDownload = $injector.get('horizon.framework.util.file.text-download'); + $scope = $injector.get('$rootScope'); + })); + + it('should return promise and it resolve filename after starting download file', function() { + var promise = textDownload.downloadTextFile('content', 'download_file_name.txt'); + $scope.$apply(); + promise.then(function(contents) { + expect(contents).toEqual('download_file_name.txt'); + }); + }); + }); +})();