Blame view
sources/apps/files/ajax/newfile.php
3.47 KB
|
03e52840d
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php
// Init owncloud
global $eventSource;
if(!OC_User::isLoggedIn()) {
exit;
}
session_write_close();
// Get the params
$dir = isset( $_REQUEST['dir'] ) ? '/'.trim($_REQUEST['dir'], '/\\') : '';
$filename = isset( $_REQUEST['filename'] ) ? trim($_REQUEST['filename'], '/\\') : '';
$content = isset( $_REQUEST['content'] ) ? $_REQUEST['content'] : '';
$source = isset( $_REQUEST['source'] ) ? trim($_REQUEST['source'], '/\\') : '';
if($source) {
$eventSource=new OC_EventSource();
} else {
OC_JSON::callCheck();
}
|
|
03e52840d
|
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
function progress($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
static $filesize = 0;
static $lastsize = 0;
global $eventSource;
switch($notification_code) {
case STREAM_NOTIFY_FILE_SIZE_IS:
$filesize = $bytes_max;
break;
case STREAM_NOTIFY_PROGRESS:
if ($bytes_transferred > 0) {
if (!isset($filesize)) {
} else {
$progress = (int)(($bytes_transferred/$filesize)*100);
|
|
31b7f2792
|
37 |
if($progress>$lastsize) { //limit the number or messages send
|
|
03e52840d
|
38 39 40 41 42 43 44 45 |
$eventSource->send('progress', $progress);
}
$lastsize=$progress;
}
}
break;
}
}
|
|
31b7f2792
|
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
$l10n = \OC_L10n::get('files');
$result = array(
'success' => false,
'data' => NULL
);
if(trim($filename) === '') {
$result['data'] = array('message' => $l10n->t('File name cannot be empty.'));
OCP\JSON::error($result);
exit();
}
if(strpos($filename, '/') !== false) {
$result['data'] = array('message' => $l10n->t('File name must not contain "/". Please choose a different name.'));
OCP\JSON::error($result);
exit();
}
//TODO why is stripslashes used on foldername in newfolder.php but not here?
$target = $dir.'/'.$filename;
if (\OC\Files\Filesystem::file_exists($target)) {
$result['data'] = array('message' => $l10n->t(
'The name %s is already used in the folder %s. Please choose a different name.',
array($filename, $dir))
);
OCP\JSON::error($result);
exit();
}
|
|
03e52840d
|
76 77 |
if($source) {
if(substr($source, 0, 8)!='https://' and substr($source, 0, 7)!='http://') {
|
|
31b7f2792
|
78 |
OCP\JSON::error(array('data' => array( 'message' => $l10n->t('Not a valid source') )));
|
|
03e52840d
|
79 80 81 82 83 |
exit();
}
$ctx = stream_context_create(null, array('notification' =>'progress'));
$sourceStream=fopen($source, 'rb', false, $ctx);
|
|
03e52840d
|
84 85 86 87 88 |
$result=\OC\Files\Filesystem::file_put_contents($target, $sourceStream);
if($result) {
$meta = \OC\Files\Filesystem::getFileInfo($target);
$mime=$meta['mimetype'];
$id = $meta['fileid'];
|
|
31b7f2792
|
89 |
$eventSource->send('success', array('mime'=>$mime, 'size'=>\OC\Files\Filesystem::filesize($target), 'id' => $id, 'etag' => $meta['etag']));
|
|
03e52840d
|
90 |
} else {
|
|
31b7f2792
|
91 |
$eventSource->send('error', $l10n->t('Error while downloading %s to %s', array($source, $target)));
|
|
03e52840d
|
92 93 94 95 |
}
$eventSource->close();
exit();
} else {
|
|
31b7f2792
|
96 97 98 99 100 101 |
$success = false;
if (!$content) {
$templateManager = OC_Helper::getFileTemplateManager();
$mimeType = OC_Helper::getMimetypeDetector()->detectPath($target);
$content = $templateManager->getTemplate($mimeType);
}
|
|
03e52840d
|
102 |
if($content) {
|
|
31b7f2792
|
103 104 105 106 107 108 109 |
$success = \OC\Files\Filesystem::file_put_contents($target, $content);
} else {
$success = \OC\Files\Filesystem::touch($target);
}
if($success) {
$meta = \OC\Files\Filesystem::getFileInfo($target);
|
|
03e52840d
|
110 111 |
$id = $meta['fileid']; $mime = $meta['mimetype']; |
|
31b7f2792
|
112 113 114 115 116 117 118 119 |
$size = $meta['size'];
OCP\JSON::success(array('data' => array(
'id' => $id,
'mime' => $mime,
'size' => $size,
'content' => $content,
'etag' => $meta['etag'],
)));
|
|
03e52840d
|
120 121 122 |
exit(); } } |
|
31b7f2792
|
123 |
OCP\JSON::error(array('data' => array( 'message' => $l10n->t('Error when creating the file') )));
|