Skip to content

Commit 31230af

Browse files
committed
added new feature- file download
1 parent 5b28345 commit 31230af

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

app/home/home.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ homeModule.config([
4040

4141
if($scope.url.match(templateUrl)){
4242
var parameter = {url: $routeParams.url,
43-
fileName: $routeParams.fileName,
44-
rootDirectory: $routeParams.rootDirectory};
43+
fileName: $routeParams.fileName,
44+
rootDirectory: $routeParams.rootDirectory};
4545
var progress = {isProcessing: $scope.isProcessing,
46-
downloadedFiles: $scope.downloadedFiles,
47-
totalFiles: $scope.totalFiles};
46+
downloadedFiles: $scope.downloadedFiles,
47+
totalFiles: $scope.totalFiles};
4848
homeService.downloadZippedFiles(parameter, progress);
4949
}else if($scope.url!=""){
50-
toastr.warning("Invalid URL",{iconClass: 'toast-down'});
50+
toastr.warning("Invalid URL", {iconClass: 'toast-down'});
5151
}
5252

5353
$scope.createDownLink = function(){
@@ -60,7 +60,7 @@ homeModule.config([
6060
if($scope.url.match(templateUrl)){
6161
$scope.downUrl = downloadUrlPrefix + $scope.url;
6262
}else if($scope.url!=""){
63-
toastr.warning("Invalid URL",{iconClass: 'toast-down'});
63+
toastr.warning("Invalid URL", {iconClass: 'toast-down'});
6464
}
6565
};
6666

@@ -86,7 +86,7 @@ homeModule.factory('homeService', [
8686
resolvedUrl.author = splitPath[1];
8787
resolvedUrl.repository = splitPath[2];
8888
resolvedUrl.branch = splitPath[4];
89-
resolvedUrl.rootFolderName = splitPath[splitPath.length-1];
89+
resolvedUrl.rootName = splitPath[splitPath.length-1];
9090
resolvedUrl.directoryPath = repoPath.substring(repoPath.indexOf(splitPath[4])+splitPath[4].length+1);
9191
resolvedUrl.urlPrefix = "https://api.github.com/repos/"+resolvedUrl.author+
9292
"/"+resolvedUrl.repository+"/contents/";
@@ -103,12 +103,12 @@ homeModule.factory('homeService', [
103103
var requestedPromises = [];
104104

105105
if(!downloadFileName || downloadFileName==""){
106-
downloadFileName = repoInfo.rootFolderName;
106+
downloadFileName = repoInfo.rootName;
107107
}
108108
if(rootDirectoryName=="false"){
109109
rootDirectoryName = "";
110110
}else if(!rootDirectoryName || rootDirectoryName=="" || rootDirectoryName=="true"){
111-
rootDirectoryName = repoInfo.rootFolderName+"/";
111+
rootDirectoryName = repoInfo.rootName+"/";
112112
}else{
113113
rootDirectoryName = rootDirectoryName+"/";
114114
}
@@ -162,23 +162,38 @@ homeModule.factory('homeService', [
162162
progress.totalFiles.val = requestedPromises.length;
163163
}
164164

165+
var downloadFile = function (url) {
166+
var zip = new JSZip();
167+
$http.get(url, {responseType: "arraybuffer"}).then(function (file){
168+
zip.file(repoInfo.rootName, file.data);
169+
zip.generateAsync({type:"blob"}).then(function(content){
170+
saveAs(content, repoInfo.rootName+".zip");
171+
});
172+
}, function(error){
173+
console.log(error);
174+
});
175+
}
176+
165177
return {
166178
downloadZippedFiles: function(parameter, progress){
167-
var resolvedUrl = resolveUrl(parameter.url);
179+
repoInfo = resolveUrl(parameter.url);
180+
downloadFileName = parameter.fileName;
181+
rootDirectoryName = parameter.rootDirectory;
168182

169-
if(!resolvedUrl.directoryPath || resolvedUrl.directoryPath==""){
170-
if(!resolvedUrl.branch || resolvedUrl.branch==""){
171-
resolvedUrl.branch="master";
183+
if(!repoInfo.directoryPath || repoInfo.directoryPath==""){
184+
if(!repoInfo.branch || repoInfo.branch==""){
185+
repoInfo.branch="master";
172186
}
173187

174-
var downloadUrl = "https://github.com/"+resolvedUrl.author+"/"+
175-
resolvedUrl.repository+"/archive/"+resolvedUrl.branch+".zip";
188+
var downloadUrl = "https://github.com/"+repoInfo.author+"/"+
189+
repoInfo.repository+"/archive/"+repoInfo.branch+".zip";
176190

177191
window.location = downloadUrl;
192+
}else if(repoInfo.rootName.indexOf(".") >= 0){
193+
var downloadUrl = "https://raw.githubusercontent.com/"+repoInfo.author+"/"+
194+
repoInfo.repository+"/"+repoInfo.branch+"/"+repoInfo.directoryPath;
195+
downloadFile(downloadUrl);
178196
}else {
179-
downloadFileName = parameter.fileName;
180-
rootDirectoryName = parameter.rootDirectory;
181-
repoInfo = resolvedUrl;
182197
downloadDir(progress);
183198
}
184199
},

0 commit comments

Comments
 (0)