|
| 1 | +/*********************************************************** |
| 2 | +* Developer: Samuel Betio ([email protected]) * |
| 3 | +* Website: https://github.com/samuelbetio/DownGit * |
| 4 | +* License: MIT License * |
| 5 | +***********************************************************/ |
| 6 | + |
| 7 | +'use strict'; |
| 8 | + |
| 9 | +var homeModule = angular.module('homeModule', [ |
| 10 | + 'ngRoute', |
| 11 | +]); |
| 12 | + |
| 13 | +homeModule.config([ |
| 14 | + '$routeProvider', |
| 15 | + |
| 16 | + function ($routeProvider) { |
| 17 | + $routeProvider |
| 18 | + .when('/home', { |
| 19 | + templateUrl: 'app/home/home.html', |
| 20 | + controller: [ |
| 21 | + '$scope', |
| 22 | + '$routeParams', |
| 23 | + '$location', |
| 24 | + 'toastr', |
| 25 | + 'homeService', |
| 26 | + |
| 27 | + function($scope, $routeParams, $location, toastr, homeService){ |
| 28 | + $scope.downUrl=""; |
| 29 | + $scope.url=""; |
| 30 | + $scope.isProcessing={val: false}; |
| 31 | + $scope.downloadedFiles={val: 0}; |
| 32 | + $scope.totalFiles={val: 0}; |
| 33 | + |
| 34 | + var templateUrl = "github.com"; |
| 35 | + var downloadUrlPrefix = "https://samuelbetio.github.io/DownGit/#/home?url="; |
| 36 | + |
| 37 | + if($routeParams.url){ |
| 38 | + $scope.url=$routeParams.url; |
| 39 | + } |
| 40 | + |
| 41 | + if($scope.url.match(templateUrl)){ |
| 42 | + var progress = {isProcessing: $scope.isProcessing, |
| 43 | + downloadedFiles: $scope.downloadedFiles, |
| 44 | + totalFiles: $scope.totalFiles}; |
| 45 | + homeService.downloadZippedFiles($scope.url, progress); |
| 46 | + }else if($scope.url!=""){ |
| 47 | + toastr.warning("Invalid URL",{iconClass: 'toast-down'}); |
| 48 | + } |
| 49 | + |
| 50 | + $scope.createDownLink = function(){ |
| 51 | + $scope.downUrl=""; |
| 52 | + |
| 53 | + if(!$scope.url){ |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if($scope.url.match(templateUrl)){ |
| 58 | + $scope.downUrl = downloadUrlPrefix + $scope.url; |
| 59 | + }else if($scope.url!=""){ |
| 60 | + toastr.warning("Invalid URL",{iconClass: 'toast-down'}); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + |
| 65 | + }], |
| 66 | + }); |
| 67 | + } |
| 68 | +]); |
| 69 | + |
| 70 | +homeModule.factory('homeService', [ |
| 71 | + '$http', |
| 72 | + '$q', |
| 73 | + |
| 74 | + function ($http, $q) { |
| 75 | + var urlPrefix = ""; |
| 76 | + var urlPostfix = ""; |
| 77 | + |
| 78 | + var resolveUrl = function(url){ |
| 79 | + var repoPath = new URL(url).pathname; |
| 80 | + var splitPath = repoPath.split("/", 5); |
| 81 | + |
| 82 | + var resolvedUrl = {}; |
| 83 | + resolvedUrl.author = splitPath[1]; |
| 84 | + resolvedUrl.repository = splitPath[2]; |
| 85 | + resolvedUrl.branch = splitPath[4]; |
| 86 | + resolvedUrl.directoryPath = repoPath.split(resolvedUrl.branch+"/", 2)[1]; |
| 87 | + |
| 88 | + return resolvedUrl; |
| 89 | + } |
| 90 | + |
| 91 | + var downloadDir = function(resolvedUrl, progress){ |
| 92 | + progress.isProcessing.val=true; |
| 93 | + urlPrefix = "https://api.github.com/repos/"+resolvedUrl.author+ |
| 94 | + "/"+resolvedUrl.repository+"/contents/"; |
| 95 | + urlPostfix = "?ref="+resolvedUrl.branch; |
| 96 | + |
| 97 | + var dirPaths = []; |
| 98 | + var files = []; |
| 99 | + var requestedPromises = []; |
| 100 | + |
| 101 | + dirPaths.push(resolvedUrl.directoryPath); |
| 102 | + mapFileAndDirectory(dirPaths, files, requestedPromises, progress, resolvedUrl); |
| 103 | + } |
| 104 | + |
| 105 | + var mapFileAndDirectory = function(dirPaths, files, requestedPromises, progress, resolvedUrl){ |
| 106 | + $http.get(urlPrefix+dirPaths.pop()+urlPostfix).then(function (response){ |
| 107 | + for (var i=response.data.length-1; i>=0; i--){ |
| 108 | + if(response.data[i].type=="dir"){ |
| 109 | + dirPaths.push(response.data[i].path); |
| 110 | + }else{ |
| 111 | + getFile(response.data[i].path, response.data[i].download_url, |
| 112 | + files, requestedPromises, progress); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if(dirPaths.length<=0){ |
| 117 | + downloadFiles(files, requestedPromises, progress, resolvedUrl); |
| 118 | + }else{ |
| 119 | + mapFileAndDirectory(dirPaths, files, requestedPromises, progress, resolvedUrl); |
| 120 | + } |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + var downloadFiles = function(files, requestedPromises, progress, resolvedUrl){ |
| 125 | + var dirSplits = resolvedUrl.directoryPath.split("/"); |
| 126 | + var downloadFileName = decodeURI(dirSplits[dirSplits.length-1]); |
| 127 | + |
| 128 | + var zip = new JSZip(); |
| 129 | + $q.all(requestedPromises).then(function(data) { |
| 130 | + for(var i=files.length-1; i>=0; i--){ |
| 131 | + zip.file(downloadFileName+"/"+files[i].path.split(downloadFileName+"/")[1], |
| 132 | + files[i].data); |
| 133 | + } |
| 134 | + |
| 135 | + progress.isProcessing.val=false; |
| 136 | + zip.generateAsync({type:"blob"}).then(function(content) { |
| 137 | + saveAs(content, downloadFileName+".zip"); |
| 138 | + }); |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + var getFile = function (path, url, files, requestedPromises, progress) { |
| 143 | + var promise = $http.get(url, {responseType: "arraybuffer"}).then(function (file){ |
| 144 | + files.push({path:path, data:file.data}); |
| 145 | + progress.downloadedFiles.val = files.length; |
| 146 | + }, function(error){ |
| 147 | + console.log(error); |
| 148 | + }); |
| 149 | + requestedPromises.push(promise); |
| 150 | + progress.totalFiles.val = requestedPromises.length; |
| 151 | + } |
| 152 | + |
| 153 | + return { |
| 154 | + downloadZippedFiles: function(url, progress){ |
| 155 | + var resolvedUrl = resolveUrl(url); |
| 156 | + |
| 157 | + if(!resolvedUrl.directoryPath || resolvedUrl.directoryPath==""){ |
| 158 | + if(!resolvedUrl.branch || resolvedUrl.branch==""){ |
| 159 | + resolvedUrl.branch="master"; |
| 160 | + } |
| 161 | + |
| 162 | + var downloadUrl = "https://github.com/"+resolvedUrl.author+"/"+ |
| 163 | + resolvedUrl.repository+"/archive/"+resolvedUrl.branch+".zip"; |
| 164 | + |
| 165 | + window.location = downloadUrl; |
| 166 | + }else { |
| 167 | + downloadDir(resolvedUrl, progress); |
| 168 | + } |
| 169 | + }, |
| 170 | + }; |
| 171 | + } |
| 172 | +]); |
| 173 | + |
0 commit comments