newfile.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // Init owncloud
  3. global $eventSource;
  4. if(!OC_User::isLoggedIn()) {
  5. exit;
  6. }
  7. session_write_close();
  8. // Get the params
  9. $dir = isset( $_REQUEST['dir'] ) ? '/'.trim($_REQUEST['dir'], '/\\') : '';
  10. $filename = isset( $_REQUEST['filename'] ) ? trim($_REQUEST['filename'], '/\\') : '';
  11. $content = isset( $_REQUEST['content'] ) ? $_REQUEST['content'] : '';
  12. $source = isset( $_REQUEST['source'] ) ? trim($_REQUEST['source'], '/\\') : '';
  13. if($source) {
  14. $eventSource=new OC_EventSource();
  15. } else {
  16. OC_JSON::callCheck();
  17. }
  18. if($filename == '') {
  19. OCP\JSON::error(array("data" => array( "message" => "Empty Filename" )));
  20. exit();
  21. }
  22. if(strpos($filename, '/')!==false) {
  23. OCP\JSON::error(array("data" => array( "message" => "Invalid Filename" )));
  24. exit();
  25. }
  26. function progress($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
  27. static $filesize = 0;
  28. static $lastsize = 0;
  29. global $eventSource;
  30. switch($notification_code) {
  31. case STREAM_NOTIFY_FILE_SIZE_IS:
  32. $filesize = $bytes_max;
  33. break;
  34. case STREAM_NOTIFY_PROGRESS:
  35. if ($bytes_transferred > 0) {
  36. if (!isset($filesize)) {
  37. } else {
  38. $progress = (int)(($bytes_transferred/$filesize)*100);
  39. if($progress>$lastsize) {//limit the number or messages send
  40. $eventSource->send('progress', $progress);
  41. }
  42. $lastsize=$progress;
  43. }
  44. }
  45. break;
  46. }
  47. }
  48. $target = $dir.'/'.$filename;
  49. if($source) {
  50. if(substr($source, 0, 8)!='https://' and substr($source, 0, 7)!='http://') {
  51. OCP\JSON::error(array("data" => array( "message" => "Not a valid source" )));
  52. exit();
  53. }
  54. $ctx = stream_context_create(null, array('notification' =>'progress'));
  55. $sourceStream=fopen($source, 'rb', false, $ctx);
  56. $result=\OC\Files\Filesystem::file_put_contents($target, $sourceStream);
  57. if($result) {
  58. $meta = \OC\Files\Filesystem::getFileInfo($target);
  59. $mime=$meta['mimetype'];
  60. $id = $meta['fileid'];
  61. $eventSource->send('success', array('mime'=>$mime, 'size'=>\OC\Files\Filesystem::filesize($target), 'id' => $id));
  62. } else {
  63. $eventSource->send('error', "Error while downloading ".$source. ' to '.$target);
  64. }
  65. $eventSource->close();
  66. exit();
  67. } else {
  68. $success = false;
  69. if (!$content) {
  70. $templateManager = OC_Helper::getFileTemplateManager();
  71. $mimeType = OC_Helper::getMimetypeDetector()->detectPath($target);
  72. $content = $templateManager->getTemplate($mimeType);
  73. }
  74. if($content) {
  75. $success = \OC\Files\Filesystem::file_put_contents($target, $content);
  76. } else {
  77. $success = \OC\Files\Filesystem::touch($target);
  78. }
  79. if($success) {
  80. $meta = \OC\Files\Filesystem::getFileInfo($target);
  81. $id = $meta['fileid'];
  82. $mime = $meta['mimetype'];
  83. $size = $meta['size'];
  84. OCP\JSON::success(array('data' => array(
  85. 'id' => $id,
  86. 'mime' => $mime,
  87. 'size' => $size,
  88. 'content' => $content,
  89. )));
  90. exit();
  91. }
  92. }
  93. OCP\JSON::error(array("data" => array( "message" => "Error when creating the file" )));