Skip to content

Commit 09f4fb5

Browse files
author
Charles-Antoine Mathieu
authored
Merge pull request src-d#222 from camathieu/1.2.1
Fix yubikey download
2 parents e35dd7a + bffc65d commit 09f4fb5

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

server/public/js/app.js

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -438,19 +438,40 @@ plik.controller('MainCtrl', ['$scope', '$api', '$config', '$route', '$location',
438438
// Parse URI
439439
var url = new URL(window.location.origin + uri);
440440

441-
var regex = /^.*\/(file|stream|archive)\/(.*?)\/(.*?)\/(.*)$/;
442-
var match = regex.exec(url.pathname);
443-
if ( !match && match.length != 5 ) {
444-
$dialog.alert({status: 0, message: "Unable to get uploadId from yubikey redirect"});
445-
$location.search({});
446-
$location.hash("");
447-
return;
441+
var mode;
442+
var uploadId;
443+
var fileId;
444+
var fileName;
445+
if(url.pathname.startsWith("/archive")) {
446+
var regex = /^.*\/(archive)\/(.*?)\/(.*)$/;
447+
var match = regex.exec(url.pathname);
448+
if (!match || match.length !== 4) {
449+
$dialog.alert({status: 0, message: "Unable to get upload from yubikey redirect"});
450+
$location.search({});
451+
$location.hash("");
452+
return;
453+
}
454+
455+
mode = match[1];
456+
uploadId = match[2];
457+
fileName = match[3];
458+
} else {
459+
var regex = /^.*\/(file|stream)\/(.*?)\/(.*?)\/(.*)$/;
460+
var match = regex.exec(url.pathname);
461+
if (!match || match.length !== 5) {
462+
$dialog.alert({status: 0, message: "Unable to get upload from yubikey redirect"});
463+
$location.search({});
464+
$location.hash("");
465+
return;
466+
}
467+
468+
mode = match[1];
469+
uploadId = match[2];
470+
fileId = match[3];
471+
fileName = match[4];
448472
}
449473

450-
var mode = match[1];
451-
var uploadId = match[2];
452-
var fileId = match[3];
453-
var fileName = match[4];
474+
fileName = decodeURIComponent(fileName);
454475

455476
var download = false;
456477
if (url.searchParams.get("dl") == 1) {
@@ -462,7 +483,7 @@ plik.controller('MainCtrl', ['$scope', '$api', '$config', '$route', '$location',
462483
// And I don't want the user to be asked for two tokens.
463484
// https://github.com/root-gg/plik/issues/215.
464485

465-
$scope.downloadWithYubikey(mode, uploadId, fileId, fileName, download);
486+
downloadWithYubikey(mode, uploadId, fileId, fileName, download);
466487
} else {
467488
var code = $location.search().errcode;
468489
$dialog.alert({status: code, message: err});
@@ -775,6 +796,10 @@ plik.controller('MainCtrl', ['$scope', '$api', '$config', '$route', '$location',
775796
return filesize(size, {base: 2});
776797
};
777798

799+
$scope.getMode = function() {
800+
return $scope.upload.stream ? "stream" : "file";
801+
}
802+
778803
// Build file download URL
779804
var getFileUrl = function(mode, uploadID, fileID, fileName, yubikeyToken, dl) {
780805
var domain = $scope.config.downloadDomain ? $scope.config.downloadDomain : $api.base;
@@ -799,8 +824,7 @@ plik.controller('MainCtrl', ['$scope', '$api', '$config', '$route', '$location',
799824
// Return file download URL
800825
$scope.getFileUrl = function (file, dl) {
801826
if (!file || !file.metadata) return;
802-
var mode = $scope.upload.stream ? "stream" : "file";
803-
return getFileUrl(mode, $scope.upload.id, file.metadata.id, file.metadata.fileName, null, dl);
827+
return getFileUrl($scope.getMode(), $scope.upload.id, file.metadata.id, file.metadata.fileName, null, dl);
804828
};
805829

806830
// Return zip archive download URL
@@ -893,22 +917,32 @@ plik.controller('MainCtrl', ['$scope', '$api', '$config', '$route', '$location',
893917
};
894918

895919
// Yubikey OTP download dialog
896-
$scope.downloadWithYubikey = function (mode, uploadID, fileId, fileName, dl) {
920+
downloadWithYubikey = function (mode, uploadID, fileID, fileName, dl) {
897921
$dialog.openDialog({
898922
backdrop: true,
899923
backdropClick: true,
900924
templateUrl: 'partials/yubikey.html',
901925
controller: 'YubikeyController'
902926
}).result.then(
903927
function (token) {
904-
// Redirect to file download URL with yubikey token
905-
var url = getFileUrl(mode, uploadID, fileId, fileName, token, dl);
928+
var url = getFileUrl(mode, uploadID, fileID, fileName, token, dl);
906929
window.location.replace(url);
907930
}, function () {
908931
// Avoid "Possibly unhandled rejection"
909932
});
910933
};
911934

935+
// Download file with Yubikey OTP dialog
936+
$scope.downloadFileWithYubikey = function(file, dl) {
937+
if (!file || !file.metadata) return;
938+
downloadWithYubikey($scope.getMode(), $scope.upload.id, file.metadata.id, file.metadata.fileName, dl);
939+
};
940+
941+
// Download archive with Yubikey OTP dialog
942+
$scope.downloadArchiveWithYubikey = function(file, dl) {
943+
downloadWithYubikey("archive", $scope.upload.id, null, "archive.zip", dl);
944+
};
945+
912946
$scope.ttlUnits = ["days", "hours", "minutes"];
913947
$scope.ttlUnit = "days";
914948
$scope.ttlValue = 30;

server/public/partials/main.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
</a>
154154
<!-- WITH YUBIKEY -->
155155
<button type="button" class="btn btn-lg btn-primary btn-block"
156-
ng-show="upload.protectedByYubikey" ng-click="downloadWithYubikey(getZipArchiveUrl())">
156+
ng-show="upload.protectedByYubikey" ng-click="downloadArchiveWithYubikey()">
157157
<i class="glyphicon glyphicon-cloud-download"></i> Zip archive
158158
</button>
159159
</div>
@@ -289,7 +289,7 @@
289289
<!-- DOWNLOAD WITH YUBIKEY -->
290290
<button title="Download" type="button" class="btn btn-success"
291291
ng-show="upload.protectedByYubikey"
292-
ng-click="downloadWithYubikey(getFileUrl(file))"><span
292+
ng-click="downloadFileWithYubikey(file)"><span
293293
class="glyphicon glyphicon-cloud-download"></span><span
294294
class="hidden-xs hidden-sm hidden-md"> Download</span></button>
295295
<!-- QR CODE -->

0 commit comments

Comments
 (0)