/** * Search file(s) in directory * * @param array $patterns * @param string $directory * @param boolean $recurse * @return array * @throws Exception */ function searchInDir(array $patterns, $directory, $recurse = FALSE) { $css_list = array(); if (is_dir($directory) === FALSE) { throw new Exception('The directory "' . $directory . '" doesn\'t exist'); } $dir_handle = opendir($directory); while (FALSE !== ($filename = readdir($dir_handle))) { if ('.' == $filename || '..' == $filename) { continue; } $path = $directory . '/' . $filename; if (is_dir($path) && $recurse) { $css_list = array_merge($css_list, searchInDir($patterns, $path, $recurse)); continue; } foreach ($patterns as $p) { if (preg_match('/' . $p . '/i', $filename)) { $css_list[] = $path; } } } closedir($dir_handle); return $css_list; }
require_once dirname(__FILE__) . '/../inc/config.php'; require_once dirname(__FILE__) . '/../inc/functions.inc.php'; try { $guid = get('id'); if ($guid == '') { throw new Exception('The parameter is not correct'); } if (is_dir(PROJECT_BUILD_DIR . '/' . $guid) === FALSE) { throw new Exception('The build doesn\'t exist'); } $pathZipFile = PROJECT_TMP_DIR . '/' . uniqid(time()) . '.zip'; $zip = new ZipArchive(); if ($zip->open($pathZipFile, ZipArchive::CREATE) !== TRUE) { throw new Exception('Zip file creation failed'); } $zip = zip_directory(PROJECT_BUILD_DIR . '/' . $guid, $zip); $zip->close(); if (file_exists($pathZipFile) !== TRUE) { throw new Exception("The file doesn't exist"); } $res = searchInDir(array('\\.(js)$'), PROJECT_BUILD_DIR . '/' . $guid, TRUE); $zipFilename = str_replace('.min.js', '.zip', basename($res[0])); header('Content-Type: application/zip'); header('Content-Disposition: attachment; filename="' . $zipFilename . '"'); header('Content-Length: ' . filesize($pathZipFile)); readfile($pathZipFile); unlink($pathZipFile); exit; } catch (Exception $e) { echo $e->getMessage(); }
$zip->close(); $res = searchInDir(array('\\.(js)$'), $build_directory, TRUE); $zipFilename = str_replace('.min.js', '.zip', basename($res[0])); header('Content-Type: application/zip'); header('Content-Disposition: attachment; filename="' . $zipFilename . '"'); header('Content-Length: ' . filesize($pathZipFile)); readfile($pathZipFile); unlink($pathZipFile); exit; } else { if ($format == 'code') { $typeFile = 'js'; if (strtolower(param('file')) == 'css') { $typeFile = 'css'; } $files = searchInDir(array('\\.' . $typeFile . '$'), $build_directory, TRUE); echo file_get_contents($files[0]); } else { if ($format == 'json') { // Return Json object echo json_encode(array('status' => 'success', 'result' => array('guid' => $guid, 'filenames' => array_map(function ($path) { $relativePath = getRelativePath(WINK_DIRECTORY, $path); $baseURL = 'http://' . $_SERVER['SERVER_NAME'] . '/' . basename(realpath(WINK_DIRECTORY)) . '/'; return rel2abs($relativePath, $baseURL); }, searchInDir(array('\\.(css|js)$'), $build_directory, TRUE))))); } } } } catch (Exception $e) { echo json_encode(array('status' => 'fail', 'result' => array('code' => $e->getCode(), 'message' => $e->getMessage()))); }