Browse Source

Working upload

Zachary Kotlarek 12 years ago
parent
commit
51e3cbb337
1 changed files with 137 additions and 18 deletions
  1. 137 18
      backup/opendrive.php

+ 137 - 18
backup/opendrive.php

@@ -1,9 +1,12 @@
 <?php
 <?php
 
 
 # Debug
 # Debug
-$DEBUG = false;
+$DEBUG = 0;
 if ($_ENV['DEBUG']) {
 if ($_ENV['DEBUG']) {
-	$DEBUG = true;
+	$DEBUG = intval($_ENV['DEBUG']);
+	if (!$DEBUG) {
+		$DEBUG = 1;
+	}
 }
 }
 
 
 # On-disk config
 # On-disk config
@@ -13,6 +16,15 @@ require_once '.secrets';
 $API_BASE = 'https://dev.opendrive.com/api/v1';
 $API_BASE = 'https://dev.opendrive.com/api/v1';
 $API_VERSION = 10;
 $API_VERSION = 10;
 
 
+# PHP version support
+if (!function_exists('curl_file_create')) {
+	function curl_file_create($filename, $mimetype = '', $postname = '') {
+		return '@' . $filename . ';filename=' .
+		($postname ?: basename($filename)) .
+		($mimetype ? ';type=' . $mimetype : '');
+	}
+}
+
 # Open the DB connection
 # Open the DB connection
 function dbOpen() {
 function dbOpen() {
 	global $DB_STRING;
 	global $DB_STRING;
@@ -43,8 +55,7 @@ function basePath($path) {
 	return basename(pathCleanup($path));
 	return basename(pathCleanup($path));
 }
 }
 
 
-# POST the provided data to OpenDrive
-function curlPost($url, $data) {
+function curlPostRaw($url, $data, $header = NULL) {
 	global $API_BASE;
 	global $API_BASE;
 	global $CA_PATH;
 	global $CA_PATH;
 	global $DEBUG;
 	global $DEBUG;
@@ -52,17 +63,27 @@ function curlPost($url, $data) {
 	# Setup cURL
 	# Setup cURL
 	$ch = curl_init($API_BASE . $url);
 	$ch = curl_init($API_BASE . $url);
 	curl_setopt_array($ch, array(
 	curl_setopt_array($ch, array(
-		CURLOPT_VERBOSE => $DEBUG,
 		CURLOPT_CAINFO => $CA_PATH,
 		CURLOPT_CAINFO => $CA_PATH,
 		CURLOPT_POST => true,
 		CURLOPT_POST => true,
 		CURLOPT_RETURNTRANSFER => true,
 		CURLOPT_RETURNTRANSFER => true,
-		CURLOPT_HTTPHEADER => array(
-			'Content-Type: application/json'
-		),
-		CURLOPT_POSTFIELDS => json_encode($data)
+		CURLOPT_POSTFIELDS => $data
 	));
 	));
+	if ($DEBUG >= 3) {
+		curl_setopt($ch, CURLOPT_VERBOSE, true);
+	}
+	if ($header != NULL) {
+		if (!is_array($header)) {
+			$header = array($header);
+		}
+		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
+	}
 
 
 	# Send the request
 	# Send the request
+	if ($DEBUG >= 2) {
+		echo "Request: \n";
+		print_r($data);
+		echo "\n";
+	}
 	$response = curl_exec($ch);
 	$response = curl_exec($ch);
 
 
 	# Check for errors
 	# Check for errors
@@ -71,7 +92,14 @@ function curlPost($url, $data) {
 	}
 	}
 
 
 	# Return
 	# Return
-	$responseData = json_decode($response, TRUE);
+	return($response);
+}
+
+# POST the provided data to OpenDrive
+function curlPost($url, $data) {
+	$data = json_encode($data);
+	$response = curlPostRaw($url, $data, 'Content-Type: application/json');
+	$responseData = json_decode($response, true);
 	return($responseData);
 	return($responseData);
 }
 }
 
 
@@ -84,12 +112,19 @@ function curlGet($url) {
 	# Setup cURL
 	# Setup cURL
 	$ch = curl_init($API_BASE . $url);
 	$ch = curl_init($API_BASE . $url);
 	curl_setopt_array($ch, array(
 	curl_setopt_array($ch, array(
-		CURLOPT_VERBOSE => $DEBUG,
 		CURLOPT_CAINFO => $CA_PATH,
 		CURLOPT_CAINFO => $CA_PATH,
 		CURLOPT_RETURNTRANSFER => true
 		CURLOPT_RETURNTRANSFER => true
 	));
 	));
+	if ($DEBUG >= 3) {
+		curl_setopt($ch, CURLOPT_VERBOSE, true);
+	}
 
 
 	# Send the request
 	# Send the request
+	if ($DEBUG >= 2) {
+		echo "Request: \n";
+		print_r($data);
+		echo "\n";
+	}
 	$response = curl_exec($ch);
 	$response = curl_exec($ch);
 
 
 	# Check for errors
 	# Check for errors
@@ -98,7 +133,7 @@ function curlGet($url) {
 	}
 	}
 
 
 	# Return
 	# Return
-	$responseData = json_decode($response, TRUE);
+	$responseData = json_decode($response, true);
 	return($responseData);
 	return($responseData);
 }
 }
 
 
@@ -158,17 +193,19 @@ function parentID($session, $path) {
 	if ($parent != '.') {
 	if ($parent != '.') {
 		$parentID = folderID($session, $parent);
 		$parentID = folderID($session, $parent);
 	}
 	}
-	if ($parentID === false) {
-		return false;
-	}
 	return $parentID;
 	return $parentID;
 }
 }
 
 
 # Create a folder
 # Create a folder
 function mkFolder($session, $path) {
 function mkFolder($session, $path) {
+	global $DEBUG;
+
 	# Find the parent ID (if any)
 	# Find the parent ID (if any)
 	$parentID = parentID($session, $path);
 	$parentID = parentID($session, $path);
 	if ($parentID === false) {
 	if ($parentID === false) {
+		if ($DEBUG) {
+			echo 'Invalid parent for path: ' . $path . "\n";
+		}
 		return false;
 		return false;
 	}
 	}
 
 
@@ -242,12 +279,94 @@ function fileInfo($session, $path) {
 
 
 # Upload the provided file to the given path
 # Upload the provided file to the given path
 function fileUpload($session, $path, $file) {
 function fileUpload($session, $path, $file) {
+	global $DEBUG;
+
+	# Find the local file
+	$stat = @stat($file);
+	if ($stat === false) {
+		if ($DEBUG) {
+			echo 'Unable to stat: ' . $file . "\n";
+		}
+		return false;
+	}
+
+	# Find the parent ID
+	$parentID = parentID($session, $path);
+	if ($parentID === false) {
+		if ($DEBUG) {
+			echo 'No parent for path: ' . $path . "\n";
+		}
+		return false;
+	}
+
+	# Allocate
+	$data = array(
+		'session_id'	=> $session,
+		'folder_id'	=> $parentID,
+		'file_name'	=> basePath($path),
+		'file_size'	=> $stat['size']
+	);
+	$response = curlPost('/upload/create_file.json', $data);
+	$id = $response['FileId'];
+	if (!$id) {
+		if ($DEBUG) {
+			echo 'Unable to allocate: ' . $path . "\n";
+		}
+		return false;
+	}
+
+	# Open
+	$data = array(
+		'session_id'	=> $session,
+		'file_id'	=> $id,
+		'file_size'	=> $stat['size']
+	);
+	$response = curlPost('/upload/open_file_upload.json', $data);
+	$tmpPath = $response['TempLocation'];
+	if (!$tmpPath) {
+		if ($DEBUG) {
+			echo 'Unable to open: ' . $path . "\n";
+		}
+		return false;
+	}
+
+	# Upload
+	$cfile = curl_file_create($file, 'application/octet-stream', basePath($file));
 	$data = array(
 	$data = array(
 		'session_id'	=> $session,
 		'session_id'	=> $session,
-		'folder_id'	=> $folderID,
-		'file_name'	=> $name,
-		'file_size'	=> $size
+		'file_id'	=> $id,
+		'temp_location'	=> $tmpPath,
+		'chunk_offset'	=> 0,
+		'chunk_size'	=> $stat['size'],
+		'file_data'	=> $cfile
 	);
 	);
+	$response = curlPostRaw('/upload/upload_file_chunk.json', $data, 'Expect:');
+	if ($response) {
+		$response = json_decode($response, true);
+	}
+	if (!$response || $response['TotalWritten'] != $stat['size']) {
+		if ($DEBUG) {
+			echo 'Unable to upload: ' . $path . "\n";
+		}
+		return false;
+	}
+
+	# Close
+	$data = array(
+		'session_id'	=> $session,
+		'file_id'	=> $id,
+		'temp_location'	=> $tmpPath,
+		'file_time'	=> $stat['mtime'],
+		'file_size'	=> $stat['size']
+	);
+	$response = curlPost('/upload/close_file_upload.json', $data);
+	if ($response && !$response['DirUpdateTime']) {
+		if ($DEBUG) {
+			echo 'Unable to close: ' . $path . "\n";
+		}
+		return false;
+	}
+	return true;
 }
 }
 
 
 ?>
 ?>