function download_open_export_file($filename) { $filepath = \Pressbooks\Modules\Export\Export::getExportFolder() . $filename; if (!is_readable($filepath)) { // Cannot read file wp_die(__('File not found', 'pressbooks-textbook') . ": {$filename}", '', array('response' => 404)); } // Force download set_time_limit(0); header('Content-Description: File Transfer'); header('Content-Type: ' . \Pressbooks\Modules\Export\Export::mimeType($filepath)); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($filepath)); @ob_clean(); flush(); while (@ob_end_flush()) { } // Fix out-of-memory problem readfile($filepath); exit; }
/** * Truncate the exports directory, delete old files. * * @param int $max * @param string $dir fullpath to the Exports fo * lder. (optional) */ function truncate_exports($max, $dir = null) { if (!$dir) { $dir = \PressBooks\Modules\Export\Export::getExportFolder(); } else { $dir = rtrim($dir, '/') . '/'; } $max = absint($max); $files = group_exports($dir); $i = 1; foreach ($files as $date => $exports) { if ($i > $max) { foreach ($exports as $export) { $export = realpath($dir . $export); WP_Filesystem(); unlink($export); } } ++$i; } }
/** * Scan the export directory, return latest of each file type * * @return array */ function latest_exports() { $suffix = array('._3.epub', '.epub', '.pdf', '.mobi', '.hpub', '.icml', '.html', '.xml', '._vanilla.xml', '._oss.pdf', '.odt'); $dir = \Pressbooks\Modules\Export\Export::getExportFolder(); $files = array(); // group by extension, sort by date newest first foreach (\Pressbooks\Utility\scandir_by_date($dir) as $file) { // only interested in the part of filename starting with the timestamp preg_match('/-\\d{10,11}(.*)/', $file, $matches); // grab the first captured parenthisized subpattern $ext = $matches[1]; $files[$ext][] = $file; } // get only one of the latest of each type $latest = array(); foreach ($suffix as $value) { if (array_key_exists($value, $files)) { $latest[$value] = $files[$value][0]; } } // @TODO filter these results against user prefs return $latest; }