/** * @param string $prefix * @param string $suffix * @return string */ public static function generate($prefix = "file", $suffix = "") { do { $temp_file = self::$temp_path . "/{$prefix}_" . md5(rand(0, 1000000000)) . $suffix; } while (file_exists($temp_file)); Logger::printf("Generating temp file %s", $temp_file); self::$temp_files[] = $temp_file; return $temp_file; }
public function doGet() { Songs::wipeOldPreviews(); FileServer::removeUnused(); FileServer::removeDead(); $limit = 30; while ($limit-- > 0) { set_time_limit(30); (new SelectQuery(TSongs::_NAME))->select(TSongs::FILE_NAME, TSongs::FILE_ID, TSongs::ID)->where(TSongs::PEAKS_ID . " IS NULL")->where(TSongs::FILE_ID . " IS NOT NULL")->limit(1)->eachRow(function ($row) { Logger::printf("Creating peaks for file: %s", $row[TSongs::FILE_NAME]); $peaks = WaveformGenerator::generate(FileServer::getFileUsingId($row[TSongs::FILE_ID])); $file_id = FileServer::registerByContent(json_encode($peaks), "application/json"); SongDao::updateSongUsingId($row[TSongs::ID], [TSongs::PEAKS_ID => $file_id]); }); sleep(1); } }
/** * Reads audio file record from database and generates audio preview. */ public function preview() { Logger::printf("Requested preview for track %s", $this->track_id); if ($this->hasPreview()) { Logger::printf("Track preview is available (file_id is %s)", $this->track_data[TSongs::PREVIEW_ID]); FileServer::sendToClient($this->track_data[TSongs::PREVIEW_ID]); return; } Logger::printf("Track preview is unavailable"); Logger::printf("Generating new track preview in real time"); header("Content-Type: " . PREVIEW_MIME); $temp_file = TempFileProvider::generate("preview", ".mp3"); $filename = FileServer::getFileUsingId($this->track_data[TSongs::FILE_ID]); $command_template = "%s -i %s -bufsize 256k -vn -ab 128k -ac 2 -acodec libmp3lame -f mp3 - | tee %s"; $command = sprintf($command_template, $this->settings->get("tools", "ffmpeg_cmd"), escapeshellarg($filename), escapeshellarg($temp_file)); passthru($command); $temp_file_id = FileServer::register($temp_file, PREVIEW_MIME); Logger::printf("New preview registered => %s", $temp_file_id); SongDao::updateSongUsingId($this->track_id, [TSongs::PREVIEW_ID => $temp_file_id]); }
<?php use app\core\router\Router; if (empty($_SERVER['PHP_AUTH_USER']) || empty($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_USER'] != "guest" || $_SERVER['PHP_AUTH_PW'] != "please") { header('WWW-Authenticate: Basic realm="Site is under construction"'); header('HTTP/1.0 401 Unauthorized'); echo "Sorry, you haven't access to this resource."; exit; } $used_before = memory_get_usage(); require_once "app/loader.php"; $router = Router::getInstance(); $router->run(); $used_after = memory_get_usage(); \app\core\logging\Logger::printf("Memory used: %d", $used_after - $used_before);
public static function wipeOldPreviews() { foreach (SongDao::getListOfUnusedPreviews() as $song) { Logger::printf("Wiping old track preview (file id %s)", $song[TSongs::PREVIEW_ID]); FileServer::unregister($song[TSongs::PREVIEW_ID]); SongDao::updateSongUsingId($song[TSongs::ID], [TSongs::PREVIEW_ID => null]); } }
/** * @param $query * @param $params * @throws ApplicationException * @return \PDOStatement */ private function createResource($query, $params = null) { $queryString = $this->queryQuote($query, $params); Logger::printf($queryString); /** * @var PDOStatement $resource */ $resource = Event::applyFilters("database.pdo.statement.prepare", $this->pdo->prepare($queryString)); if ($resource === false) { throw new ApplicationException($this->pdo->errorInfo()[2]); } $resource->execute(); Event::callEventListeners("database.pdo.statement.executed", $resource); if ($resource->errorCode() !== "00000") { throw new ApplicationException($resource->errorInfo()[2]); } return $resource; }
public static function removeUnused() { $unused_files = (new SelectQuery(TFiles::_NAME))->where(TFiles::USED, 0)->fetchAll(); foreach ($unused_files as $file) { Logger::printf("Deleting unused file with id = %s, size = %d", $file[TFiles::ID], $file[TFiles::SIZE]); FileServer::findFileUsingId($file[TFiles::ID])->filter("file_exists")->then("unlink"); (new DeleteQuery(TFiles::_NAME))->where(TFiles::ID, $file[TFiles::ID])->update(); } }