function coalFromFile($filename, $csump) { global $l; $l->a("Started coalFromFile<br>"); global $coalVersion; $status = 0; $type = null; $size = null; $stat = null; $smtime = null; $stats = null; $ctime = null; $mtime = null; $atime = null; if (file_exists($filename)) { $type = filetype($filename); $size = filesize($filename); $stat = stat($filename); $smtime = base64_encode($stat['mtime']); $stats = bin2hex(var_export($stat, true)); $ctime = base64_encode(filectime($filename)); $mtime = base64_encode(filemtime($filename)); $atime = base64_encode(fileatime($filename)); } else { $status = 3; } $csum = new Csum(null, $filename); if (!$csump->matches($csum)) { $status = 57; } $chunks = ''; //$l->a("DATA IN: ".file_get_contents($filename)); $fhandle = fopen($filename, "r"); while (ftell($fhandle) < $size) { $chunk = fread($fhandle, 4194304); $c = new Csum($chunk); $chunkInfo = insertChunk($chunk, $c); $chunkId = $chunkInfo['id']; $status = status_add($status, $chunkInfo['status']); $cins = ','; if (strlen($chunks) == 0) { $cins = ''; } $chunks = $chunks . $cins . $chunkId; } fclose($fhandle); $chunks_csum = new Csum($chunks); $details = array('csum' => $csum->export(), 'chunks' => $chunks, 'chunks_csum' => $chunks_csum->export(), 'filename' => $filename, 'type' => $type, 'size' => $size, 'smtime' => $smtime, 'stats' => $stats, 'ctime' => $ctime, 'mtime' => $mtime, 'atime' => $atime); return array('filename' => $filename, 'details' => $details, 'status' => $status); }
function store($data, $csumb) { $csum = new Csum($data); if (!$csum->matches($csumb)) { return null; } $status = 0; //Why I'm not doing this type of deduplication: It could lead to inaccurate metadata about the coal. //Ya know, screw that. Coal *shouldn't support* file uploads — that should be handled by higher level software. I'm putting this back in for now, and just remember that the Coal file-level metadata is only an ugly, non-archival-quality, incomplete hack for while Ember doesn't exist yet to take care of that. $db = new FractureDB('futuqiur_ember'); $potentialDuplicates = $db->getColumnsUH('strings', 'id', 'md5', $csum->md5); foreach ($potentialDuplicates as $potential) { //echo 'duplicate testing'; $potentialRecord = retrieveCoal($potential['id']); if (!is_null($potentialRecord)) { $potentialData = $potentialRecord['data']; $potentialCsum = Csum_import($potentialRecord['csum']); if ($potentialData === $data && matches($csum, $potentialCsum)) { $duplicateId = $potential['id']; return array('id' => $duplicateId, 'csum' => $potentialRecord['csum'], 'status' => $status); } } } $db->close(); //echo 'gotten here'; $filename = 'coal_temp/' . uniqid() . '.cstf'; file_put_contents($filename, $data); return insertCoal($filename, $csum); }