From 869c990ed921e5e2dd7870b20d326fdf1ffc6260 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Fri, 4 May 2012 15:32:35 +0200 Subject: [PATCH] Initial commit --- LICENSE | 13 +++++ repair.php | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tahoe.sh | 85 +++++++++++++++++++++++++++++++++ url.py | 10 ++++ 4 files changed, 244 insertions(+) create mode 100644 LICENSE create mode 100644 repair.php create mode 100644 tahoe.sh create mode 100644 url.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..45e4a66 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. diff --git a/repair.php b/repair.php new file mode 100644 index 0000000..21ce8f6 --- /dev/null +++ b/repair.php @@ -0,0 +1,136 @@ + + + + Cryto Tahoe-LAFS Storage Grid repair utility + + + + $value) + { + $variables[$key] = urlencode($value); + $variable_strings[] = "{$key}={$value}"; + } + + $post_string = implode("&", $variable_strings); + } + else + { + $post_string = ""; + } + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POST, count($variables)); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); + + $result = curl_exec($ch); + + $error = curl_error($ch); + if(empty($error)) + { + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $return_object->result = $result; + $return_object->code = $code; + return $return_object; + } + else + { + return false; + } + + curl_close($ch); + } + + if(isset($_POST['submit'])) + { + $uri = $_POST['uri']; + $process = true; + + // process repair request + if(preg_match("/http:\/\/[^\/]+\/download\/([^\/]+)(\/.*)?/i", $uri, $matches)) + { + // gateway url + $uri = $matches[1]; + } + + if(!preg_match("/^[a-zA-Z0-9=_:]+$/i", $uri)) + { + $process = false; + echo("ERROR: Your input was in an invalid format.

"); + } + + if(strpos($uri, ":") === false) + { + // probably base64 + $uri = base64_decode($uri); + + if($uri === false) + { + $process = false; + echo("ERROR: Your input was invalid. Check if it's really a valid Tahoe-LAFS gateway URL, base64-encoded URI, or plaintext URI.

"); + } + } + + if($process === true) + { + $result = curl_post("http://localhost:3456/uri/{$uri}?t=check&repair=true&output=json", false); + + if($result->code == 200) + { + $result_object = json_decode($result->result); + + $attempted = $result_object->{'repair-attempted'}; + + if($attempted) + { + $succeeded = $result_object->{'repair-successful'}; + } + else + { + $succeeded = false; + } + + if($attempted === false) + { + echo("The file is still healthy, and does not need to be repaired."); + } + elseif($attempted === true && $succeeded === false) + { + echo("Repair of the file was attempted, but FAILED."); + } + else + { + echo("Repair of the file was SUCCESSFULLY completed."); + } + + echo("
{$result->result}
"); + } + else + { + echo("ERROR: Repair failed. Your input may be in the wrong format or the server may be down.

"); + } + } + } + ?> + +
+ URI/base64/URL: + +
+ + \ No newline at end of file diff --git a/tahoe.sh b/tahoe.sh new file mode 100644 index 0000000..a1fbe67 --- /dev/null +++ b/tahoe.sh @@ -0,0 +1,85 @@ +#!/bin/bash +## Changelog +# 16-AUG-2011 | Xeross | Don't read into temp file but straight into variable +# 16-AUG-2011 | Xeross | If non-zero exit status don't run url.py +# 16-AUG-2011 | Xeross | Make remote filename for file upload optional + +##### Configuration starts here ##### + +# The URL of the Tahoe-LAFS web interface that you are trying to upload to. +tahoe="http://localhost:3456/uri/"; + +# The (write cap) URI of the directory you want to upload to by default. +URI=""; + +##### Configuration ends here ##### + +URI=${URI//:/%3A}; +fullURI=$tahoe$URI/; + +function upload_file { + local TYPE=$1 + local DATA=$2 + local DEST=$3 + + if [ "$TYPE" == "INPUT" ]; then + CAP=$(echo "$DATA" | curl --progress-bar -T - "$fullURI$DEST") + RETVAL=$? + elif [ "$TYPE" == "FILE" ]; then + CAP=$(curl --progress-bar -T "$DATA" "$fullURI$DEST") + RETVAL=$? + else + echo "Error: Unknown type passed" + exit 2 + fi + + if [ $RETVAL -ne 0 ]; then + echo "Upload failed (Exit code: $RETVAL)" + else + python url.py $CAP $DEST + fi +} + +function usage { + echo "Usage:" + echo "Upload input from stdin" + echo "# cat file.txt | ./tahoe.sh -i subfolder/filename.txt" + echo + echo "Upload specified file" + echo "# ./tahoe.sh -f file.jpg [subfolders/filename.jpg]" + echo + echo "Create new subdirectory" + echo "# ./tahoe.sh -d new_subdirectory" +} + +case $1 in + -i) + if [ $# -ne 2 ]; then + echo "Invalid syntax" + usage + exit 1 + fi + + read INPUT + upload_file "INPUT" "$INPUT" "$2" + ;; + -f) + if [ $# -eq 2 ]; then + FILENAME=$(basename $2) + elif [ $# -eq 3 ]; then + FILENAME=$3 + else + echo "Invalid syntax" + usage + exit 1 + fi + + upload_file "FILE" "$2" "$FILENAME" + ;; + -d) + curl -d "" $fullURI$2?t=mkdir + ;; + *) + usage + ;; +esac diff --git a/url.py b/url.py new file mode 100644 index 0000000..3519305 --- /dev/null +++ b/url.py @@ -0,0 +1,10 @@ +import sys,base64 + +if len(sys.argv) < 3: + print ("""\ + Usage: url.py cap filename + cap = Tahoe-LAFS readcap + filename = Actual filename of the file + """) +else: + print "http://tahoe-gateway.cryto.net:3719/download/" + base64.urlsafe_b64encode(sys.argv[1]) + "/" + sys.argv[2]