mirror of
https://github.com/claunia/cuetools.net.git
synced 2025-12-16 18:14:25 +00:00
CD parity information database
This commit is contained in:
@@ -15,10 +15,12 @@ namespace Krystalware.UploadHelper
|
||||
{
|
||||
public class HttpUploadHelper
|
||||
{
|
||||
private HttpUploadHelper()
|
||||
public EventHandler<UploadProgressEventArgs> onProgress;
|
||||
|
||||
public HttpUploadHelper()
|
||||
{ }
|
||||
|
||||
public static string Upload(string url, UploadFile[] files, NameValueCollection form)
|
||||
public string Upload(string url, UploadFile[] files, NameValueCollection form)
|
||||
{
|
||||
HttpWebResponse resp = Upload((HttpWebRequest)WebRequest.Create(url), files, form);
|
||||
|
||||
@@ -29,7 +31,7 @@ namespace Krystalware.UploadHelper
|
||||
}
|
||||
}
|
||||
|
||||
public static HttpWebResponse Upload(HttpWebRequest req, UploadFile[] files, NameValueCollection form)
|
||||
public HttpWebResponse Upload(HttpWebRequest req, UploadFile[] files, NameValueCollection form)
|
||||
{
|
||||
List<MimePart> mimeParts = new List<MimePart>();
|
||||
|
||||
@@ -84,16 +86,25 @@ namespace Krystalware.UploadHelper
|
||||
|
||||
using (Stream s = req.GetRequestStream())
|
||||
{
|
||||
long pos = 0;
|
||||
foreach (MimePart part in mimeParts)
|
||||
{
|
||||
s.Write(part.Header, 0, part.Header.Length);
|
||||
pos += part.Header.Length;
|
||||
|
||||
while ((read = part.Data.Read(buffer, 0, buffer.Length)) > 0)
|
||||
s.Write(buffer, 0, read);
|
||||
while ((read = part.Data.Read(buffer, 0, buffer.Length)) > 0)
|
||||
{
|
||||
if (onProgress != null)
|
||||
onProgress(part,
|
||||
new UploadProgressEventArgs(req.RequestUri.AbsoluteUri, ((double)pos)/contentLength));
|
||||
s.Write(buffer, 0, read);
|
||||
pos += read;
|
||||
}
|
||||
|
||||
part.Data.Dispose();
|
||||
|
||||
s.Write(afterFile, 0, afterFile.Length);
|
||||
pos += afterFile.Length;
|
||||
}
|
||||
|
||||
s.Write(_footer, 0, _footer.Length);
|
||||
@@ -111,4 +122,16 @@ namespace Krystalware.UploadHelper
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class UploadProgressEventArgs: EventArgs
|
||||
{
|
||||
public string uri;
|
||||
public double percent;
|
||||
|
||||
public UploadProgressEventArgs(string uri, double percent)
|
||||
{
|
||||
this.uri = uri;
|
||||
this.percent = percent;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,61 @@ function filesize_h($size, $dec = 1)
|
||||
return round($size, $dec) . ' ' . $sizes[$i];
|
||||
}
|
||||
|
||||
function BigEndian2Int($byte_word, $signed = false) {
|
||||
|
||||
$int_value = 0;
|
||||
$byte_wordlen = strlen($byte_word);
|
||||
|
||||
for ($i = 0; $i < $byte_wordlen; $i++) {
|
||||
$int_value += ord($byte_word{$i}) * pow(256, ($byte_wordlen - 1 - $i));
|
||||
}
|
||||
|
||||
if ($signed) {
|
||||
$sign_mask_bit = 0x80 << (8 * ($byte_wordlen - 1));
|
||||
if ($int_value & $sign_mask_bit) {
|
||||
$int_value = 0 - ($int_value & ($sign_mask_bit - 1));
|
||||
}
|
||||
}
|
||||
|
||||
return $int_value;
|
||||
}
|
||||
|
||||
function get_chunk_offset($fp, $offset, $filelen, $names, $namepos)
|
||||
{
|
||||
// echo $offset, ":", $filelen, ":", $names, ":", $namepos, ":", count($names), "<br>";
|
||||
if ($namepos >= count($names))
|
||||
return $offset;
|
||||
while ($offset < $filelen) {
|
||||
fseek($fp, $offset, SEEK_SET);
|
||||
$atom_header = fread($fp, 8);
|
||||
$atom_size = BigEndian2Int(substr($atom_header, 0, 4));
|
||||
$atom_name = substr($atom_header, 4, 4);
|
||||
// echo $atom_size, ":", $atom_name, ":", $names[$namepos], '<br>';
|
||||
if ($names[$namepos] == $atom_name)
|
||||
return get_chunk_offset($fp, $offset + 8, $filelen, $names, $namepos + 1);
|
||||
$offset += $atom_size;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
function chunk_offset($fp, $offset, $filelen, $path)
|
||||
{
|
||||
$names = explode(".", $path);
|
||||
return get_chunk_offset($fp, $offset, $filelen, $names, 0);
|
||||
}
|
||||
|
||||
function read_chunk($fp, $offset, $filelen, $path, $len)
|
||||
{
|
||||
$offset = chunk_offset($fp, $offset, $filelen, $path);
|
||||
if ($offset < 0) return;
|
||||
fseek($fp, $offset, SEEK_SET);
|
||||
return fread($fp, $len);
|
||||
}
|
||||
|
||||
function read_int($fp, $offset, $filelen, $path)
|
||||
{
|
||||
return BigEndian2Int(read_chunk($fp, $offset, $filelen, $path, 4));
|
||||
}
|
||||
|
||||
$file = $_FILES['uploadedfile'];
|
||||
|
||||
@@ -61,17 +116,43 @@ $file = $_FILES['uploadedfile'];
|
||||
echo "Error ", $file['error'];
|
||||
}
|
||||
|
||||
//if ($_SERVER['HTTP_USER_AGENT'] != "CUETools 205") {
|
||||
// echo "user agent ", $_SERVER['HTTP_USER_AGENT'], " is not allowed";
|
||||
// return;
|
||||
//}
|
||||
|
||||
$tmpname = $file['tmp_name'];
|
||||
$size = (@file_exists($tmpname)) ? filesize($tmpname) : 0;
|
||||
if ($size == 0) {
|
||||
echo "no file uploaded";
|
||||
return;
|
||||
}
|
||||
|
||||
$fp = fopen($tmpname, 'rb');
|
||||
$head = read_chunk($fp, 0, $size, 'CTDB.HEAD', 20);
|
||||
$npar = read_int($fp, 0, $size, 'CTDB.DISC.NPAR');
|
||||
$version = BigEndian2Int(substr($head,0,4));
|
||||
$disccount = BigEndian2Int(substr($head,4,4));
|
||||
$total = BigEndian2Int(substr($head,8,4));
|
||||
printf("npar=%d, disccount=%d, total=%d,", $npar, $disccount, $total);
|
||||
fclose($fp);
|
||||
|
||||
$id = $_POST['id'];
|
||||
$err = sscanf($id, "%d-%x-%x-%x", $tracks, $id1, $id2, $cddbid);
|
||||
$target_path = sprintf("parity/%x/%x/%x", $id1 & 15, ($id1 >> 4) & 15, ($id1 >> 8) & 15);
|
||||
$target_file = sprintf("%s/dBCT-%03d-%08x-%08x-%08x.bin", $target_path, $tracks, $id1, $id2, $cddbid);
|
||||
$err = sscanf($id, "%03d-%04x%04x-%04x%04x-%04x%04x", $tracks, $id1a, $id1b, $id2a, $id2b, $cddbida, $cddbidb);
|
||||
$parsedid = sprintf("%03d-%04x%04x-%04x%04x-%04x%04x", $tracks, $id1a, $id1b, $id2a, $id2b, $cddbida, $cddbidb);
|
||||
if ($id != $parsedid) {
|
||||
echo "bad id ", $id;
|
||||
return;
|
||||
}
|
||||
$target_path = sprintf("parity/%x/%x/%x", $id1b & 15, ($id1b >> 4) & 15, ($id1b >> 8) & 15);
|
||||
$target_file = sprintf("%s/dBCT-%s.bin", $target_path, $parsedid);
|
||||
|
||||
@mkdir($target_path, 0777, true);
|
||||
|
||||
if(move_uploaded_file($file['tmp_name'], $target_file)) {
|
||||
echo "The file ". $target_file. " has been uploaded";
|
||||
printf("%s has been uploaded", $parsedid);
|
||||
} else{
|
||||
echo "There was an error uploading the file, please try again!";
|
||||
echo "there was an error uploading the file, please try again!";
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user