Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Fischer <bantu@owncloud.com>2014-02-09 04:25:33 +0400
committerAndreas Fischer <bantu@owncloud.com>2014-05-29 18:26:01 +0400
commitc8fa1fd68784c48c1df537310f16d6753f79b029 (patch)
treeda53d4744785eafbfc77e50a835904eb9e5f6d32 /lib/private/largefilehelper.php
parent3f8f8027d25ab39283d0ab13afcf7252dde8f240 (diff)
Refactor Large File handling code.
Diffstat (limited to 'lib/private/largefilehelper.php')
-rw-r--r--lib/private/largefilehelper.php103
1 files changed, 103 insertions, 0 deletions
diff --git a/lib/private/largefilehelper.php b/lib/private/largefilehelper.php
new file mode 100644
index 00000000000..ca8f7522177
--- /dev/null
+++ b/lib/private/largefilehelper.php
@@ -0,0 +1,103 @@
+<?php
+/**
+ * Copyright (c) 2014 Andreas Fischer <bantu@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC;
+
+/**
+ * Helper class for large files on 32-bit platforms.
+ */
+class LargeFileHelper {
+ /**
+ * @brief Tries to get the filesize of a file via various workarounds that
+ * even work for large files on 32-bit platforms.
+ *
+ * @param string $filename Path to the file.
+ *
+ * @return null|int|float Number of bytes as number (float or int) or
+ * null on failure.
+ */
+ public function getFilesize($filename) {
+ $filesize = $this->getFilesizeViaCurl($filename);
+ if (!is_null($filesize)) {
+ return $filesize;
+ }
+ $filesize = $this->getFilesizeViaCOM($filename);
+ if (!is_null($filesize)) {
+ return $filesize;
+ }
+ $filesize = $this->getFilesizeViaExec($filename);
+ if (!is_null($filesize)) {
+ return $filesize;
+ }
+ return null;
+ }
+
+ /**
+ * @brief Tries to get the filesize of a file via a CURL HEAD request.
+ *
+ * @param string $filename Path to the file.
+ *
+ * @return null|int|float Number of bytes as number (float or int) or
+ * null on failure.
+ */
+ public function getFilesizeViaCurl($filename) {
+ if (function_exists('curl_init')) {
+ $ch = curl_init("file://$filename");
+ curl_setopt($ch, CURLOPT_NOBODY, true);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_HEADER, true);
+ $data = curl_exec($ch);
+ curl_close($ch);
+ if ($data !== false) {
+ $matches = array();
+ preg_match('/Content-Length: (\d+)/', $data, $matches);
+ if (isset($matches[1])) {
+ return 0 + $matches[1];
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @brief Tries to get the filesize of a file via the Windows DOM extension.
+ *
+ * @param string $filename Path to the file.
+ *
+ * @return null|int|float Number of bytes as number (float or int) or
+ * null on failure.
+ */
+ public function getFilesizeViaCOM($filename) {
+ if (class_exists('COM')) {
+ $fsobj = new \COM("Scripting.FileSystemObject");
+ $file = $fsobj->GetFile($filename);
+ return 0 + $file->Size;
+ }
+ return null;
+ }
+
+ /**
+ * @brief Tries to get the filesize of a file via an exec() call.
+ *
+ * @param string $filename Path to the file.
+ *
+ * @return null|int|float Number of bytes as number (float or int) or
+ * null on failure.
+ */
+ public function getFilesizeViaExec($filename) {
+ if (\OC_Helper::is_function_enabled('exec')) {
+ $os = strtolower(php_uname('s'));
+ if (strpos($os, 'linux') !== false) {
+ return 0 + exec('stat -c %s ' . escapeshellarg($filename));
+ } else if (strpos($os, 'bsd') !== false) {
+ return 0 + exec('stat -f %z ' . escapeshellarg($filename));
+ }
+ }
+ return null;
+ }
+}