Play images and video from Synology PhotoStation server

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?PHP
  2. set_time_limit(0);
  3. require_once('file.inc.php');
  4. class FileAPI extends WebAPI
  5. {
  6. function __construct()
  7. {
  8. parent::__construct(SZ_WEBAPI_API_DESCRIPTION_PATH);
  9. }
  10. protected function Process()
  11. {
  12. $this->Init();
  13. csSYNOPhotoMisc::CheckSessionTimeOut(true);
  14. if (!strcasecmp($this->method, "uploadphoto")) {
  15. $this->UploadFile(true);
  16. }
  17. if (!strcasecmp($this->method, "uploadvideo")) {
  18. $this->UploadFile(false);
  19. }
  20. }
  21. private function Init()
  22. {
  23. csSYNOPhotoMisc::PhotoSessionStart();
  24. $_SESSION[SYNOPHOTO_ADMIN_USER]['security_identifier'] = csSYNOPhotoMisc::GetSessionIdentifier();
  25. if (isset($_REQUEST['session'])) {
  26. csSYNOPhotoMisc::RestoreSession();
  27. }
  28. csSYNOPhotoDB::GetDBInstance()->SetSessionCache(true);
  29. }
  30. private function UploadFile($isPhoto)
  31. {
  32. $ret = false;
  33. /* return when lack of params */
  34. if (!isset($_REQUEST['dest_folder_path']) || !isset($_REQUEST['duplicate']) || !isset($_REQUEST['filename']) || !isset($_REQUEST['mtime'])) {
  35. $this->SetError(PHOTOSTATION_FILE_BAD_PARAMS);
  36. goto End;
  37. }
  38. /* set required params */
  39. $destShareName = $_REQUEST['dest_folder_path'];
  40. if ('' === $destShareName) {
  41. if (!csSYNOPhotoMisc::CheckAlbumUploadable('/')) {
  42. $this->SetError(PHOTOSTATION_FILE_ACCESS_DENY);
  43. goto End;
  44. }
  45. } elseif (!csSYNOPhotoMisc::CheckAlbumUploadable($destShareName)) {
  46. $this->SetError(PHOTOSTATION_FILE_ACCESS_DENY);
  47. goto End;
  48. }
  49. $fileName = $_REQUEST['filename'];
  50. if ($isPhoto && !csSYNOPhotoMisc::IsPhotoFile($fileName)) {
  51. $this->SetError(PHOTOSTATION_FILE_FILE_EXT_ERR);
  52. goto End;
  53. }
  54. if (!$isPhoto && !csSYNOPhotoMisc::IsVideoFile($fileName)) {
  55. $this->SetError(PHOTOSTATION_FILE_FILE_EXT_ERR);
  56. goto End;
  57. }
  58. $destDir = SYNOPHOTO_SERVICE_REAL_DIR . "/" . $destShareName;
  59. if (!csSynoPhotoMisc::CheckPathValid($destDir)) {
  60. $this->SetError(PHOTOSTATION_FILE_BAD_PARAMS);
  61. goto End;
  62. }
  63. if (!file_exists($destDir)) {
  64. $this->SetError(PHOTOSTATION_FILE_DIR_NOT_EXISTS);
  65. goto End;
  66. }
  67. $mtime = $_REQUEST['mtime'];
  68. $dup = $_REQUEST['duplicate'];
  69. $overwrite = ('ignore' === $dup) ? 0 : (('overwrite' === $dup) ? 1 : 2); // default is rename
  70. /* set all uploaded file data */
  71. $hasFile = false;
  72. $files = array();
  73. $allowed_upload_form_names = array('original', 'thumb_small', 'thumb_large', 'high', 'medium', 'low', 'mobile', 'iphone', 'android', 'flv');
  74. foreach($allowed_upload_form_names as $name) {
  75. $files[$name] = null;
  76. if (isset($_FILES[$name])) {
  77. if ($_FILES[$name]['error'] > 0) { // files is not uploaded successfully by form upload
  78. $this->SetError(PHOTOSTATION_FILE_UPLOAD_ERROR);
  79. goto End;
  80. }
  81. $files[$name] = $_FILES[$name];
  82. }
  83. $hasFile = true;
  84. }
  85. if (!$hasFile) {
  86. $this->SetError(PHOTOSTATION_FILE_NO_FILE);
  87. goto End;
  88. }
  89. $destPath = $destDir . "/" . $fileName;
  90. if (@file_exists($destPath)) {
  91. if (0 === $overwrite) {
  92. //ignore
  93. $ret = false;
  94. goto End;
  95. } else if (2 === $overwrite) {
  96. //rename
  97. $dir = pathinfo($destPath, PATHINFO_DIRNAME);
  98. $name = pathinfo($destPath, PATHINFO_FILENAME);
  99. $ext = pathinfo($destPath, PATHINFO_EXTENSION);
  100. $rename_suffix = 0;
  101. $rename_path = $destPath;
  102. while (file_exists($rename_path)) {
  103. $rename_path = "$dir/$name" . "_" . (++$rename_suffix) . ".$ext";
  104. }
  105. $destPath = $rename_path;
  106. }
  107. }
  108. $IsOldEAFilePrefix = csSYNOPhotoMisc::IsOldEAFilePrefix();
  109. /* set all destination file path */
  110. $destEADir = SYNOPhotoEA::getEADirPath($destPath);
  111. $smallPath = $destEADir . "/" . SYNOPhotoEA::getFilename(SYNOPhotoEA::FILE_THUMB_M, $IsOldEAFilePrefix);
  112. $largePath = $destEADir . "/" . SYNOPhotoEA::getFilename(SYNOPhotoEA::FILE_THUMB_XL, $IsOldEAFilePrefix);
  113. $vHighPath = $destEADir . "/" . SYNOPhotoEA::getFilename(SYNOPhotoEA::FILE_FILM_H_MP4, $IsOldEAFilePrefix);
  114. $vMediumPath = $destEADir . "/" . SYNOPhotoEA::getFilename(SYNOPhotoEA::FILE_FILM_M_MP4, $IsOldEAFilePrefix);
  115. $vLowPath = $destEADir . "/" . SYNOPhotoEA::getFilename(SYNOPhotoEA::FILE_FILM_L_MP4, $IsOldEAFilePrefix);
  116. $vFlvPath = $destEADir . "/" . SYNOPhotoEA::getFilename(SYNOPhotoEA::FILE_FILM_FLV, $IsOldEAFilePrefix);
  117. $vMobilePath = $destEADir . "/" . SYNOPhotoEA::getFilename(SYNOPhotoEA::FILE_FILM_MOBILE_MP4, $IsOldEAFilePrefix);
  118. $vIPhonePath = $destEADir . "/" . SYNOPhotoEA::getFilename(SYNOPhotoEA::FILE_FILM_MOBILE_IPHONE, $IsOldEAFilePrefix);
  119. $vAndroidPath = $destEADir . "/" . SYNOPhotoEA::getFilename(SYNOPhotoEA::FILE_FILM_MOBILE_ANDROID, $IsOldEAFilePrefix);
  120. @mkdir($destDir, 0777, true);
  121. @mkdir($destEADir, 0777, true);
  122. /* write all existed files */
  123. $this->writeFile($files['thumb_small']['tmp_name'], $smallPath);
  124. $this->writeFile($files['thumb_large']['tmp_name'], $largePath);
  125. if (!$isPhoto) {
  126. $this->writeFile($files['high']['tmp_name'], $vHighPath);
  127. $this->writeFile($files['medium']['tmp_name'], $vMediumPath);
  128. $this->writeFile($files['low']['tmp_name'], $vLowPath);
  129. $this->writeFile($files['flv']['tmp_name'], $vFlvPath);
  130. $this->writeFile($files['mobile']['tmp_name'], $vMobilePath);
  131. $this->writeFile($files['iphone']['tmp_name'], $vIPhonePath);
  132. $this->writeFile($files['android']['tmp_name'], $vAndroidPath);
  133. }
  134. $ret = $this->writeFile($files['original']['tmp_name'], $destPath);
  135. /* trigger index */
  136. if (!empty($files['original']) && $ret) {
  137. @touch($destPath, $mtime / 1000);
  138. $path_part = pathinfo($rename_path ? $rename_path : $fileName);
  139. if (isset($_REQUEST['title'])) {
  140. $path_part['title'] = $_REQUEST['title'];
  141. }
  142. if (isset($_REQUEST['description'])) {
  143. $path_part['description'] = $_REQUEST['description'];
  144. }
  145. $dbPath = SYNOPHOTO_SERVICE_REAL_DIR_PATH . trim($destShareName . "/" . $path_part['basename'], "/");
  146. File::AddFile($dbPath, $destPath, $path_part['title'], $path_part['description']);
  147. }
  148. PhotoLog::Add($fileName." was uploaded to album ".$destShareName." successfully.", true, strtolower(GetLoginUsername()));
  149. $ret = true;
  150. End:
  151. return $ret;
  152. }
  153. private function writeFile($tmpPath, $destPath)
  154. {
  155. if (empty($tmpPath)) {
  156. return false;
  157. }
  158. if (!is_uploaded_file($tmpPath)) {
  159. return false;
  160. }
  161. if (!@move_uploaded_file($tmpPath, $destPath)) {
  162. return false;
  163. }
  164. @chmod($destPath, 0777);
  165. return true;
  166. }
  167. }
  168. $api = new FileAPI();
  169. $api->Run();