dashboard.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. let panel = {};
  2. panel.page;
  3. panel.username;
  4. panel.token = localStorage.token;
  5. panel.filesView = localStorage.filesView;
  6. panel.preparePage = function(){
  7. if(!panel.token) return window.location = '/auth';
  8. panel.verifyToken(panel.token, true);
  9. };
  10. panel.verifyToken = function(token, reloadOnError){
  11. if(reloadOnError === undefined)
  12. reloadOnError = false;
  13. axios.post('/api/tokens/verify', {
  14. token: token
  15. })
  16. .then(function (response) {
  17. if(response.data.success === false){
  18. swal({
  19. title: "An error ocurred",
  20. text: response.data.description,
  21. type: "error"
  22. }, function(){
  23. if(reloadOnError){
  24. localStorage.removeItem("token");
  25. location.location = '/auth';
  26. }
  27. });
  28. return;
  29. }
  30. axios.defaults.headers.common['token'] = token;
  31. localStorage.token = token;
  32. panel.token = token;
  33. panel.username = response.data.username;
  34. return panel.prepareDashboard();
  35. })
  36. .catch(function (error) {
  37. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  38. console.log(error);
  39. });
  40. };
  41. panel.prepareDashboard = function(){
  42. panel.page = document.getElementById('page');
  43. if(panel.username != 'root') document.getElementById('itemUserAdd').style.display = 'none';
  44. document.getElementById('dashboard').style.display = 'block';
  45. document.getElementById('itemUploads').addEventListener('click', function(){
  46. panel.setActiveMenu(this);
  47. });
  48. document.getElementById('itemManageGallery').addEventListener('click', function(){
  49. panel.setActiveMenu(this);
  50. });
  51. document.getElementById('itemTokens').addEventListener('click', function(){
  52. panel.setActiveMenu(this);
  53. });
  54. document.getElementById('itemPassword').addEventListener('click', function(){
  55. panel.setActiveMenu(this);
  56. });
  57. document.getElementById('itemLogout').innerHTML = `Logout ( ${panel.username} )`;
  58. panel.getAlbumsSidebar();
  59. };
  60. panel.logout = function(){
  61. localStorage.removeItem("token");
  62. location.reload('/');
  63. };
  64. panel.sizePrefixes = [[1, "B"], [1000, "kB"], [1000000, "MB"], [1000000000, "GB"]];
  65. panel.sizeToString = function(size){
  66. let [baseLimit, baseUnit] = panel.sizePrefixes[0];
  67. for(let [limit, unit] of panel.sizePrefixes){
  68. if(size >= limit)
  69. [baseLimit, baseUnit] = [limit, unit];
  70. else
  71. break;
  72. }
  73. return `${(size / baseLimit).toFixed(1)} ${baseUnit}`;
  74. };
  75. panel.getUploads = function(album = undefined, page = undefined){
  76. if(page === undefined) page = 0;
  77. let url = '/api/uploads/' + page;
  78. if(album !== undefined)
  79. url = '/api/album/' + album + '/' + page;
  80. axios.get(url).then(function (response) {
  81. if(response.data.success === false){
  82. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  83. else return swal("An error ocurred", response.data.description, "error");
  84. }
  85. var prevPage = 0;
  86. var nextPage = page + 1;
  87. if(response.data.files.length < 25)
  88. nextPage = page;
  89. if(page > 0) prevPage = page - 1;
  90. panel.page.innerHTML = '';
  91. var container = document.createElement('div');
  92. var pagination = `<nav class="pagination is-centered">
  93. <a class="pagination-previous" onclick="panel.getUploads(${album}, ${prevPage} )">Previous</a>
  94. <a class="pagination-next" onclick="panel.getUploads(${album}, ${nextPage} )">Next page</a>
  95. </nav>`;
  96. var listType = `
  97. <div class="columns">
  98. <div class="column">
  99. <a class="button is-small is-outlined is-danger" title="List view" onclick="panel.setFilesView('list', ${album}, ${page})">
  100. <span class="icon is-small">
  101. <i class="fa fa-list-ul"></i>
  102. </span>
  103. </a>
  104. <a class="button is-small is-outlined is-danger" title="List view" onclick="panel.setFilesView('thumbs', ${album}, ${page})">
  105. <span class="icon is-small">
  106. <i class="fa fa-th-large"></i>
  107. </span>
  108. </a>
  109. </div>
  110. </div>`;
  111. if(panel.filesView === 'thumbs'){
  112. container.innerHTML = `
  113. ${pagination}
  114. <hr>
  115. ${listType}
  116. <div class="columns is-multiline is-mobile" id="table">
  117. </div>
  118. ${pagination}
  119. `;
  120. panel.page.appendChild(container);
  121. var table = document.getElementById('table');
  122. for(var item of response.data.files){
  123. var div = document.createElement('div');
  124. div.className = "column is-2";
  125. if(item.thumb !== undefined)
  126. div.innerHTML = `<a href="${item.file}" target="_blank"><img src="${item.thumb}"/></a><a class="button is-small is-danger is-outlined" title="Delete file" onclick="panel.deleteFile(${item.id})"><span class="icon is-small"><i class="fa fa-trash-o"></i></span></a>`;
  127. else
  128. div.innerHTML = `<a href="${item.file}" target="_blank"><h1 class="title">.${item.file.split('.').pop()}</h1></a><a class="button is-small is-danger is-outlined" title="Delete file" onclick="panel.deleteFile(${item.id})"><span class="icon is-small"><i class="fa fa-trash-o"></i></span></a>`;
  129. table.appendChild(div);
  130. }
  131. }else{
  132. var albumOrUser = 'Album';
  133. if(panel.username === 'root')
  134. albumOrUser = 'User';
  135. container.innerHTML = `
  136. ${pagination}
  137. <hr>
  138. ${listType}
  139. <table class="table is-striped is-narrow is-left bold-children">
  140. <thead>
  141. <tr>
  142. <th>File</th>
  143. <th>Original name</th>
  144. <th>Size</th>
  145. <th>${albumOrUser}</th>
  146. <th>Date</th>
  147. <th></th>
  148. </tr>
  149. </thead>
  150. <tbody id="table">
  151. </tbody>
  152. </table>
  153. <hr>
  154. ${pagination}
  155. `;
  156. panel.page.appendChild(container);
  157. var table = document.getElementById('table');
  158. for(var item of response.data.files){
  159. var tr = document.createElement('tr');
  160. var displayAlbumOrUser = item.album;
  161. if(panel.username === 'root'){
  162. displayAlbumOrUser = '';
  163. if(item.username !== undefined)
  164. displayAlbumOrUser = item.username;
  165. }
  166. tr.innerHTML = `
  167. <tr>
  168. <td class="valign"><a href="${item.file}" target="_blank">${item.name}</a></td>
  169. <td class="original-name">${item.original}</td>
  170. <td class="size-display">${panel.sizeToString(item.size)}</td>
  171. <td>${displayAlbumOrUser}</td>
  172. <td class="normal">${item.date}</td>
  173. <td>
  174. <a class="button is-small is-danger is-outlined" title="Delete album" onclick="panel.deleteFile(${item.id})">
  175. <span class="icon is-small">
  176. <i class="fa fa-trash-o"></i>
  177. </span>
  178. </a>
  179. </td>
  180. </tr>
  181. `;
  182. table.appendChild(tr);
  183. //$clamp(tr.querySelector(".clamp"), {clamp: 1});
  184. }
  185. }
  186. })
  187. .catch(function (error) {
  188. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  189. console.log(error);
  190. });
  191. };
  192. panel.setFilesView = function(view, album, page){
  193. localStorage.filesView = view;
  194. panel.filesView = view;
  195. panel.getUploads(album, page);
  196. };
  197. panel.deleteFile = function(id){
  198. swal({
  199. title: "Are you sure?",
  200. text: "You wont be able to recover the file!",
  201. type: "warning",
  202. showCancelButton: true,
  203. confirmButtonColor: "#ff3860",
  204. confirmButtonText: "Yes, delete it!",
  205. closeOnConfirm: false
  206. },
  207. function(){
  208. axios.post('/api/upload/delete', {
  209. id: id
  210. })
  211. .then(function (response) {
  212. if(response.data.success === false){
  213. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  214. else return swal("An error ocurred", response.data.description, "error");
  215. }
  216. swal("Deleted!", "The file has been deleted.", "success");
  217. panel.getUploads();
  218. })
  219. .catch(function (error) {
  220. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  221. console.log(error);
  222. });
  223. }
  224. );
  225. };
  226. panel.getAlbums = function(){
  227. axios.get('/api/albums').then(function (response) {
  228. if(response.data.success === false){
  229. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  230. else return swal("An error ocurred", response.data.description, "error");
  231. }
  232. panel.page.innerHTML = '';
  233. var container = document.createElement('div');
  234. container.className = "container";
  235. container.innerHTML = `
  236. <h2 class="subtitle">Create new album</h2>
  237. <p class="control has-addons has-addons-centered">
  238. <input id="albumName" class="input" type="text" placeholder="Name">
  239. <a id="submitAlbum" class="button is-primary">Submit</a>
  240. </p>
  241. <h2 class="subtitle">List of albums</h2>
  242. <table class="table is-striped is-narrow">
  243. <thead>
  244. <tr>
  245. <th>Name</th>
  246. <th>Files</th>
  247. <th>Created At</th>
  248. <th>Public link</th>
  249. <th></th>
  250. </tr>
  251. </thead>
  252. <tbody id="table">
  253. </tbody>
  254. </table>`;
  255. panel.page.appendChild(container);
  256. var table = document.getElementById('table');
  257. for(var item of response.data.albums){
  258. var tr = document.createElement('tr');
  259. tr.innerHTML = `
  260. <tr>
  261. <th>${item.name}</th>
  262. <th>${item.files}</th>
  263. <td>${item.date}</td>
  264. <td><a href="${item.identifier}" target="_blank">Album link</a></td>
  265. <td>
  266. <a class="button is-small is-primary is-outlined" title="Edit name" onclick="panel.renameAlbum(${item.id})">
  267. <span class="icon is-small">
  268. <i class="fa fa-pencil"></i>
  269. </span>
  270. </a>
  271. <a class="button is-small is-danger is-outlined" title="Delete album" onclick="panel.deleteAlbum(${item.id})">
  272. <span class="icon is-small">
  273. <i class="fa fa-trash-o"></i>
  274. </span>
  275. </a>
  276. </td>
  277. </tr>
  278. `;
  279. table.appendChild(tr);
  280. }
  281. document.getElementById('submitAlbum').addEventListener('click', function(){
  282. panel.submitAlbum();
  283. });
  284. })
  285. .catch(function (error) {
  286. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  287. console.log(error);
  288. });
  289. };
  290. panel.renameAlbum = function(id){
  291. swal({
  292. title: "Rename album",
  293. text: "New name you want to give the album:",
  294. type: "input",
  295. showCancelButton: true,
  296. closeOnConfirm: false,
  297. animation: "slide-from-top",
  298. inputPlaceholder: "My super album"
  299. },function(inputValue){
  300. if (inputValue === false) return false;
  301. if (inputValue === "") {
  302. swal.showInputError("You need to write something!");
  303. return false;
  304. }
  305. axios.post('/api/albums/rename', {
  306. id: id,
  307. name: inputValue
  308. })
  309. .then(function (response) {
  310. if(response.data.success === false){
  311. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  312. else if(response.data.description === 'Name already in use') swal.showInputError("That name is already in use!");
  313. else swal("An error ocurred", response.data.description, "error");
  314. return;
  315. }
  316. swal("Success!", "Your album was renamed to: " + inputValue, "success");
  317. panel.getAlbumsSidebar();
  318. panel.getAlbums();
  319. })
  320. .catch(function (error) {
  321. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  322. console.log(error);
  323. });
  324. });
  325. };
  326. panel.deleteAlbum = function(id){
  327. swal({
  328. title: "Are you sure?",
  329. text: "This won't delete your files, only the album!",
  330. type: "warning",
  331. showCancelButton: true,
  332. confirmButtonColor: "#ff3860",
  333. confirmButtonText: "Yes, delete it!",
  334. closeOnConfirm: false
  335. },
  336. function(){
  337. axios.post('/api/albums/delete', {
  338. id: id
  339. })
  340. .then(function (response) {
  341. if(response.data.success === false){
  342. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  343. else return swal("An error ocurred", response.data.description, "error");
  344. }
  345. swal("Deleted!", "Your album has been deleted.", "success");
  346. panel.getAlbumsSidebar();
  347. panel.getAlbums();
  348. })
  349. .catch(function (error) {
  350. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  351. console.log(error);
  352. });
  353. }
  354. );
  355. };
  356. panel.submitAlbum = function(){
  357. axios.post('/api/albums', {
  358. name: document.getElementById('albumName').value
  359. })
  360. .then(function (response) {
  361. if(response.data.success === false){
  362. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  363. else return swal("An error ocurred", response.data.description, "error");
  364. }
  365. swal("Woohoo!", "Album was added successfully", "success");
  366. panel.getAlbumsSidebar();
  367. panel.getAlbums();
  368. })
  369. .catch(function (error) {
  370. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  371. console.log(error);
  372. });
  373. };
  374. panel.getAlbumsSidebar = function(){
  375. axios.get('/api/albums/sidebar')
  376. .then(function (response) {
  377. if(response.data.success === false){
  378. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  379. else return swal("An error ocurred", response.data.description, "error");
  380. }
  381. var albumsContainer = document.getElementById('albumsContainer');
  382. albumsContainer.innerHTML = '';
  383. if(response.data.albums === undefined) return;
  384. for(var album of response.data.albums){
  385. li = document.createElement('li');
  386. a = document.createElement('a');
  387. a.id = album.id;
  388. a.innerHTML = album.name;
  389. a.addEventListener('click', function(){
  390. panel.getAlbum(this);
  391. });
  392. li.appendChild(a);
  393. albumsContainer.appendChild(li);
  394. }
  395. })
  396. .catch(function (error) {
  397. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  398. console.log(error);
  399. });
  400. };
  401. panel.getAlbum = function(item){
  402. panel.setActiveMenu(item);
  403. panel.getUploads(item.id);
  404. };
  405. panel.changeToken = function(){
  406. axios.get('/api/tokens')
  407. .then(function (response) {
  408. if(response.data.success === false){
  409. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  410. else return swal("An error ocurred", response.data.description, "error");
  411. }
  412. panel.page.innerHTML = '';
  413. var container = document.createElement('div');
  414. container.className = "container";
  415. container.innerHTML = `
  416. <h2 class="subtitle">Manage your token</h2>
  417. <label class="label">Your current token:</label>
  418. <p class="control has-addons">
  419. <input id="token" readonly class="input is-expanded" type="text" placeholder="Your token" value="${response.data.token}">
  420. <a id="getNewToken" class="button is-primary">Request new token</a>
  421. </p>
  422. `;
  423. panel.page.appendChild(container);
  424. document.getElementById('getNewToken').addEventListener('click', function(){
  425. panel.getNewToken();
  426. });
  427. })
  428. .catch(function (error) {
  429. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  430. console.log(error);
  431. });
  432. };
  433. panel.getNewToken = function(){
  434. axios.post('/api/tokens/change')
  435. .then(function (response) {
  436. if(response.data.success === false){
  437. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  438. else return swal("An error ocurred", response.data.description, "error");
  439. }
  440. swal({
  441. title: "Woohoo!",
  442. text: 'Your token was changed successfully.',
  443. type: "success"
  444. }, function(){
  445. localStorage.token = response.data.token;
  446. location.reload();
  447. });
  448. })
  449. .catch(function (error) {
  450. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  451. console.log(error);
  452. });
  453. };
  454. panel.addUser = function(){
  455. panel.page.innerHTML = '';
  456. var container = document.createElement('div');
  457. container.className = "container";
  458. container.innerHTML = `
  459. <h2 class="subtitle">Add a new user</h2>
  460. <label class="label">Username</label>
  461. <p class="control has-addons">
  462. <input id="user" class="input is-expanded" type="text" placeholder="Username">
  463. </p>
  464. <label class="label">Password</label>
  465. <p class="control has-addons">
  466. <input id="pass" class="input is-expanded" type="password" placeholder="Password">
  467. <a id="sendAddUser" class="button is-primary">Add user</a>
  468. </p>
  469. `;
  470. panel.page.appendChild(container);
  471. document.getElementById('sendAddUser').addEventListener('click', function(){
  472. var user = document.getElementById('user').value;
  473. var pass = document.getElementById('pass').value;
  474. if(user === undefined || user === null || user === '')
  475. return swal('Error', 'You need to specify a username', 'error');
  476. if(pass === undefined || pass === null || pass === '')
  477. return swal('Error', 'You need to specify a username', 'error');
  478. axios.post('/api/register', {
  479. username: user,
  480. password: pass
  481. })
  482. .then(function (response) {
  483. if(response.data.success === false)
  484. return swal('Error', response.data.description, 'error');
  485. swal({
  486. title: "Woohoo!",
  487. text: 'User created successfully!',
  488. type: "success"
  489. }, function(){
  490. location.reload();
  491. });
  492. })
  493. .catch(function (error) {
  494. return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error');
  495. console.log(error);
  496. });
  497. });
  498. };
  499. panel.changePassword = function(){
  500. panel.page.innerHTML = '';
  501. var container = document.createElement('div');
  502. container.className = "container";
  503. container.innerHTML = `
  504. <h2 class="subtitle">Change your password</h2>
  505. <label class="label">New password:</label>
  506. <p class="control has-addons">
  507. <input id="password" class="input is-expanded" type="password" placeholder="Your new password">
  508. </p>
  509. <label class="label">Confirm password:</label>
  510. <p class="control has-addons">
  511. <input id="passwordConfirm" class="input is-expanded" type="password" placeholder="Verify your new password">
  512. <a id="sendChangePassword" class="button is-primary">Set new password</a>
  513. </p>
  514. `;
  515. panel.page.appendChild(container);
  516. document.getElementById('sendChangePassword').addEventListener('click', function(){
  517. if (document.getElementById('password').value === document.getElementById('passwordConfirm').value) {
  518. panel.sendNewPassword(document.getElementById('password').value);
  519. } else {
  520. swal({
  521. title: "Password mismatch!",
  522. text: 'Your passwords do not match, please try again.',
  523. type: "error"
  524. }, function() {
  525. panel.changePassword();
  526. });
  527. }
  528. });
  529. };
  530. panel.sendNewPassword = function(pass){
  531. axios.post('/api/password/change', {password: pass})
  532. .then(function (response) {
  533. if(response.data.success === false){
  534. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  535. else return swal("An error ocurred", response.data.description, "error");
  536. }
  537. swal({
  538. title: "Woohoo!",
  539. text: 'Your password was changed successfully.',
  540. type: "success"
  541. }, function(){
  542. location.reload();
  543. });
  544. })
  545. .catch(function (error) {
  546. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  547. console.log(error);
  548. });
  549. };
  550. panel.setActiveMenu = function(item){
  551. var menu = document.getElementById('menu');
  552. var items = menu.getElementsByTagName('a');
  553. for(var i = 0; i < items.length; i++)
  554. items[i].className = "";
  555. item.className = 'is-active';
  556. };
  557. window.onload = function () {
  558. panel.preparePage();
  559. };