dashboard.js 16 KB

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