private function _tarball($id) { if ($this->mmultipaste->id_exists($id)) { $seen = array(); $path = $this->mmultipaste->get_tarball_path($id); $archive = new \service\storage($path); if (!$archive->exists()) { $files = $this->mmultipaste->get_files($id); $total_size = 0; foreach ($files as $filedata) { $total_size += $filedata["filesize"]; } if ($total_size > $this->config->item("tarball_max_size")) { throw new \exceptions\PublicApiException("file/tarball/tarball-filesize-limit", "Tarball too large, refusing to create."); } $tmpfile = $archive->begin(); // create empty tar archive so PharData has something to open file_put_contents($tmpfile, str_repeat("", 1024 * 10)); $a = new PharData($tmpfile); foreach ($files as $filedata) { $filename = $filedata["filename"]; if (isset($seen[$filename]) && $seen[$filename]) { $filename = $filedata["id"] . "-" . $filedata["filename"]; } assert(!isset($seen[$filename])); $a->addFile($this->mfile->file($filedata["data_id"]), $filename); $seen[$filename] = true; } $archive->gzip_compress(); $archive->commit(); } // update mtime so the cronjob will keep the file for longer $lock = fopen($archive->get_file(), "r+"); flock($lock, LOCK_SH); touch($archive->get_file()); flock($lock, LOCK_UN); assert(filesize($archive->get_file()) > 0); $this->load->driver("ddownload"); $this->ddownload->serveFile($archive->get_file(), "{$id}.tar.gz", "application/x-gzip"); } }
private static function add_file_callback($id, $new_file, $filename) { $CI =& get_instance(); $hash = md5_file($new_file); $storage_id = null; $query = $CI->db->select('id, hash')->from('file_storage')->where('hash', $hash)->get()->result_array(); foreach ($query as $row) { $data_id = implode("-", array($row['hash'], $row['id'])); $old_file = $CI->mfile->file($data_id); // TODO: set $new_file if (files_are_equal($old_file, $new_file)) { $storage_id = $row["id"]; break; } } if ($storage_id === null) { $filesize = filesize($new_file); $mimetype = mimetype($new_file); $CI->db->insert("file_storage", array("filesize" => $filesize, "mimetype" => $mimetype, "hash" => $hash, "date" => time())); $storage_id = $CI->db->insert_id(); } $data_id = $hash . "-" . $storage_id; // TODO: all this doesn't have to run if the file exists. updating the mtime would be enough // that would also be better for COW filesystems $dir = $CI->mfile->folder($data_id); file_exists($dir) || mkdir($dir); $new_path = $CI->mfile->file($data_id); $dest = new \service\storage($new_path); $tmpfile = $dest->begin(); rename($new_file, $tmpfile); $dest->commit(); $CI->mfile->add_file($id, $filename, $storage_id); }