A simple file sharing site with an easy to use API and online panel.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

110 行
3.9KB

  1. var angular = require('angular');
  2. angular.module('KeyCtrl', ['KeySvc', 'AuthSvc']).controller('KeyController', ['$scope', 'KeyService', 'AuthService', function($scope, KeyService, AuthService) {
  3. // Transforms an array of period-separated properties ex. ["file.upload", "user.view", "user.ban"]
  4. // to json ex. {"file": "upload", "user": ["view", "ban"]}
  5. function scopeToObj(scope) {
  6. const res = {};
  7. for(const prop in scope) {
  8. if (scope.hasOwnProperty(prop)) {
  9. const perm = scope[prop];
  10. const prefix = perm.substr(0, perm.indexOf('.'));
  11. const postfix = perm.substr(perm.indexOf('.') + 1);
  12. if (!res[prefix]) res[prefix] = [];
  13. res[prefix].push({name: postfix});
  14. }
  15. }
  16. return res;
  17. }
  18. $scope.init = () => {
  19. AuthService.whoami(res => {
  20. $scope.scope = scopeToObj(res.scope);
  21. $scope.displayname = res.displayname;
  22. });
  23. $scope.getAllKeys();
  24. $scope.newScope = [];
  25. };
  26. // ------------ Keys ------------ ///
  27. class Key {
  28. constructor(identifier, scope, key) {
  29. this.identifier = identifier;
  30. this.scope = scopeToObj(scope);
  31. this.key = key;
  32. }
  33. }
  34. $scope.getAllKeys = () =>
  35. KeyService.getAllKeys((err, res) =>
  36. $scope.keys = res.map(key => new Key(key.identifier, key.scope, key.key)));
  37. $scope.createKey = () =>
  38. KeyService.createKey($scope.newIdentifier, $scope.newScope, (err, res) => {
  39. if (err) return;
  40. $scope.hideNewKey();
  41. $scope.getAllKeys();
  42. });
  43. $scope.deleteKey = key =>
  44. KeyService.deleteKey(key.key, (err, res) => {
  45. if (err) return;
  46. $scope.keys.splice($scope.keys.indexOf(key), 1);
  47. $scope.hideKeyInfo();
  48. $scope.currKey = {};
  49. });
  50. // Triggered when a checkbox for a permission changes.
  51. // Updates the currKeyScope object with the addition or removal.
  52. $scope.updateNewScope = function(prefix, perm) {
  53. // If the checkbox was checked
  54. if ($scope.scope[prefix][$scope.scope[prefix].indexOf(perm)].isChecked) {
  55. $scope.newScope.push(prefix + '.' + perm.name);
  56. } else {
  57. // Otherwise it was unchecked, remove it
  58. $scope.newScope.splice($scope.newScope.indexOf(prefix + '.' + perm.name), 1);
  59. }
  60. };
  61. // Hide/show new key modal dialog
  62. $scope.hideNewKey = () => $scope.newModalStyle = {};
  63. $scope.showNewKey = () => $scope.newModalStyle = {display: 'block'};
  64. // Hide/show key info modal dialog
  65. $scope.hideKeyInfo = () => $scope.infoModalStyle = {};
  66. $scope.showKeyInfo = key => {
  67. $scope.currKey = key;
  68. $scope.infoModalStyle = {display: 'block'};
  69. };
  70. function downloadData(mime, filename, data) {
  71. const dataStr = 'data:' + mime + ';charset=utf-8,' + encodeURIComponent(data);
  72. const anchor = document.createElement('a');
  73. anchor.setAttribute('href', dataStr);
  74. anchor.setAttribute('download', filename);
  75. document.body.appendChild(anchor);
  76. anchor.click();
  77. anchor.remove();
  78. }
  79. $scope.downloadBash = () => {
  80. const data =
  81. '#!/bin/bash\n' +
  82. 'curl \\\n' +
  83. ' -F key=' + $scope.currKey.key + ' \\\n' +
  84. ' -F "file=@$1" \\\n' +
  85. ' https://shrimpa.rocks/api/upload \\\n' +
  86. ' | grep -Po \'"\'"url"\'"\\s*:\\s*"\\K([^"]*)\'\n';
  87. downloadData('text/x-shellscript', 'shrimpa.rocks.sh', data);
  88. };
  89. $scope.downloadSharex = () => {
  90. const data = {
  91. RequestURL: 'https://shrimpa.rocks/api/upload',
  92. FileFormName: 'file',
  93. Arguments: {
  94. key: $scope.currKey.key
  95. },
  96. URL: '$json:url$'
  97. };
  98. downloadData('text/json', 'shrimpa.rocks.sxcu', JSON.stringify(data));
  99. };
  100. }]);