From e29b5187a4638229d619f27d584cc82831ee8be3 Mon Sep 17 00:00:00 2001 From: borislav Date: Sun, 26 Sep 2021 14:23:19 +0200 Subject: [PATCH] Limit length of file names Too long file names break the donwload process. See https://forum.chitanka.info/topic6608.html This is a quick fix, the whole download process should be refactored. A related change: http://github.com/chitanka/chitanka/commit/049381f0b2d596a593add4c95893f4269b7c8e6b --- app/Entity/Text.php | 13 +++++++++++-- app/Generator/TextDownloadService.php | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/Entity/Text.php b/app/Entity/Text.php index 459948b4..62947eff 100644 --- a/app/Entity/Text.php +++ b/app/Entity/Text.php @@ -871,13 +871,22 @@ public function getDataAsPlain() { } public function getNameForFile() { - return strtr(Setup::setting('download_file'), [ + $lengthLimits = [ + 'AUTHOR' => 50, + 'SERIES' => 50, + 'TITLE' => 100, + ]; + $parts = [ 'AUTHOR' => $this->getAuthorNameEscaped(), 'SERIES' => empty($this->series) ? '' : Stringy::createAcronym(Char::cyr2lat($this->series->getName())), 'SERNO' => $this->getSernr() ?? '', 'TITLE' => Char::cyr2lat($this->title), 'ID' => $this->getId(), - ]); + ]; + array_walk($parts, function(string &$value, string $name) use ($lengthLimits) { + $value = substr($value, 0, $lengthLimits[$name] ?? 20); + }); + return strtr(Setup::setting('download_file'), $parts); } public static function getMinRating() { diff --git a/app/Generator/TextDownloadService.php b/app/Generator/TextDownloadService.php index c85d3e23..33a95d3c 100644 --- a/app/Generator/TextDownloadService.php +++ b/app/Generator/TextDownloadService.php @@ -191,6 +191,7 @@ private function createDlFile($textIds, $format, $dlkey = null) { $this->zipFileName = "Архив от Моята библиотека - $fileCount файла-".time(); } + $this->zipFileName = substr($this->zipFileName, 0, 230); $this->zipFileName .= $fileCount > 1 ? "-$format" : $dlkey; $this->zipFileName = File::cleanFileName( Char::cyr2lat($this->zipFileName) ); $fullZipFileName = $this->zipFileName . '.zip';