storage.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCoreSamples Samples
  7. * @{
  8. */
  9. /*
  10. INSERT INTO `sys_objects_storage` (`object`, `engine`, `params`, `token_life`, `cache_control`, `levels`, `table_files`, `ext_mode`, `ext_allow`, `ext_deny`, `quota_size`, `current_size`, `quota_number`, `current_number`, `max_file_size`, `ts`) VALUES
  11. ('sample', 'Local', '', 360, 2592000, 0, 'sample', 'allow-deny', 'jpg,jpeg,jpe,gif,png,svg', '', 0, 0, 0, 0, 0, 0);
  12. CREATE TABLE IF NOT EXISTS `sample` (
  13. `id` int(11) NOT NULL AUTO_INCREMENT,
  14. `profile_id` int(10) unsigned NOT NULL,
  15. `remote_id` varchar(255) NOT NULL,
  16. `path` varchar(255) NOT NULL,
  17. `file_name` varchar(255) NOT NULL,
  18. `mime_type` varchar(128) NOT NULL,
  19. `ext` varchar(32) NOT NULL,
  20. `size` int(11) NOT NULL,
  21. `added` int(11) NOT NULL,
  22. `modified` int(11) NOT NULL,
  23. `private` int(11) NOT NULL,
  24. PRIMARY KEY (`id`),
  25. UNIQUE KEY `remote_id` (`remote_id`)
  26. );
  27. */
  28. require_once('./../inc/header.inc.php');
  29. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  30. $oTemplate = BxDolTemplate::getInstance();
  31. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  32. $oTemplate->setPageHeader ("Sample storage");
  33. $oTemplate->setPageContent ('page_main_code', PageCompMainCode());
  34. $oTemplate->getPageCode();
  35. /**
  36. * page code function
  37. */
  38. function PageCompMainCode()
  39. {
  40. ob_start();
  41. ?>
  42. <form enctype="multipart/form-data" method="POST">
  43. Choose a file to upload:
  44. <input name="file" type="file" />
  45. <br />
  46. <input type="submit" name="add" value="Upload File" />
  47. </form>
  48. <?php
  49. $iProfileId = 123;
  50. BxDolStorage::pruning(); // pruning is needed to clear expired security tokens, you can call it on cron when your server is not busy
  51. $oStorage = BxDolStorage::getObjectInstance('sys_files');
  52. $aStorageObject = $oStorage->getObjectData();
  53. echo "<b>Engine: " . $aStorageObject['engine'] . '</b><br />';
  54. echo "<b>Endpoint: " . getParam('sys_storage_s3_endpoint') . '</b><br />';
  55. // echo '<pre>reloadMimeTypesFromFile: [' . $oStorage->reloadMimeTypesFromFile(/*'/Users/alex/mime.types'*/'/etc/apache2/mime.types') . ']</pre>';
  56. if (isset($_POST['add'])) {
  57. $iId = $oStorage->storeFileFromForm($_FILES['file'], true, $iProfileId);
  58. if ($iId) {
  59. $iCount = $oStorage->afterUploadCleanup($iId, $iProfileId);
  60. echo "<hr />uploaded file id: " . $iId . "(deleted ghosts:" . $iCount . ")<hr />";
  61. } else {
  62. echo "<hr />error uploading file: " . $oStorage->getErrorString() . "<hr />";
  63. }
  64. }
  65. if (isset($_POST['delete'])) {
  66. foreach ($_POST['file_id'] as $iFileId) {
  67. $bRet = $oStorage->deleteFile($iFileId, $iProfileId);
  68. if ($bRet)
  69. echo "<hr />deleted file id: " . $iFileId . "<hr />";
  70. else
  71. echo "<hr />file deleting error: " . $oStorage->getErrorString() . "<hr />";
  72. }
  73. } elseif (isset($_POST['public']) || isset($_POST['private'])) {
  74. $isPrivate = isset($_POST['private']) && $_POST['private'] ? true : false;
  75. $sAction = $isPrivate ? 'private' : 'public';
  76. foreach ($_POST['file_id'] as $iFileId) {
  77. $bRet = $oStorage->setFilePrivate($iFileId, $isPrivate);
  78. if ($bRet)
  79. echo "<hr /> making <b>$sAction</b> file id: " . $iFileId . "<hr />";
  80. else
  81. echo "<hr />file making <b>$sAction</b> error: " . $oStorage->getErrorString() . "<hr />";
  82. }
  83. }
  84. $a = $oStorage->getFilesAll();
  85. echo "<h2>Files List:</h2> <hr />";
  86. foreach ($a as $r)
  87. echo $r['file_name'] . '(private:[' . $oStorage->isFilePrivate($r['id']) . ']) : <img src="' . $oStorage->getFileUrlById($r['id']) . '" style="max-width:150px;" /> <hr />';
  88. echo '<form method="POST">';
  89. foreach ($a as $r)
  90. echo '<input type="checkbox" name="file_id[]" value="' . $r['id'] . '" /> ' . $r['file_name'] . '<br />';
  91. echo '<input type="submit" name="delete" value="Delete" /> <input type="submit" name="public" value="Set Public" /> <input type="submit" name="private" value="Set Private" />';
  92. echo '</form>';
  93. $s = ob_get_clean();
  94. return DesignBoxContent("Sample storage", $s, BX_DB_PADDING_DEF);
  95. }
  96. /** @} */