|
| 1 | +/* |
| 2 | + * Bittorrent Client using Qt and libtorrent. |
| 3 | + * Copyright (C) 2026 The qBittorrent project |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU General Public License |
| 7 | + * as published by the Free Software Foundation; either version 2 |
| 8 | + * of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | + * |
| 19 | + * In addition, as a special exception, the copyright holders give permission to |
| 20 | + * link this program with the OpenSSL project's "OpenSSL" library (or with |
| 21 | + * modified versions of it that use the same license as the "OpenSSL" library), |
| 22 | + * and distribute the linked executables. You must obey the GNU General Public |
| 23 | + * License in all respects for all of the code used other than "OpenSSL". If you |
| 24 | + * modify file(s), you may extend this exception to your version of the file(s), |
| 25 | + * but you are not obligated to do so. If you do not wish to do so, delete this |
| 26 | + * exception statement from your version. |
| 27 | + */ |
| 28 | + |
| 29 | +#include "toplevelpayload.h" |
| 30 | + |
| 31 | +#include <algorithm> |
| 32 | + |
| 33 | +#include "base/global.h" |
| 34 | +#include "base/utils/fs.h" |
| 35 | + |
| 36 | +namespace |
| 37 | +{ |
| 38 | + constexpr int HASH_TAG_HEX_LEN = 12; |
| 39 | + constexpr int MAX_COMPONENT_LEN = 255; |
| 40 | + |
| 41 | + PathList renameRootFolder(PathList filePaths, const QString &oldName, const QString &newName) |
| 42 | + { |
| 43 | + const Path oldRoot {oldName}; |
| 44 | + const Path newRoot {newName}; |
| 45 | + for (Path &filePath : filePaths) |
| 46 | + { |
| 47 | + if (filePath == oldRoot) |
| 48 | + filePath = newRoot; |
| 49 | + else if (filePath.hasAncestor(oldRoot)) |
| 50 | + filePath = newRoot / oldRoot.relativePathOf(filePath); |
| 51 | + } |
| 52 | + return filePaths; |
| 53 | + } |
| 54 | + |
| 55 | + // Sanitize and append tag, leaving room for the tag within MAX_COMPONENT_LEN. |
| 56 | + QString hashedComponent(const QString &name, const QString &tag) |
| 57 | + { |
| 58 | + QString base = Utils::Fs::toValidFileName(name.trimmed()); |
| 59 | + if (base.isEmpty()) |
| 60 | + base = u"Torrent"_s; |
| 61 | + |
| 62 | + if (base.endsWith(tag)) |
| 63 | + return base; |
| 64 | + |
| 65 | + const int maxBaseLen = std::max(1, MAX_COMPONENT_LEN - static_cast<int>(tag.size())); |
| 66 | + if (base.size() > maxBaseLen) |
| 67 | + base = base.left(maxBaseLen); |
| 68 | + |
| 69 | + return base + tag; |
| 70 | + } |
| 71 | + |
| 72 | + QString originalNameForHashDir(const PathList &filePaths, const QString &torrentName, const QString &tag) |
| 73 | + { |
| 74 | + QString base = torrentName.trimmed(); |
| 75 | + if (base.isEmpty()) |
| 76 | + { |
| 77 | + const Path root = Path::findRootFolder(filePaths); |
| 78 | + if (!root.isEmpty()) |
| 79 | + base = root.toString(); |
| 80 | + else if (!filePaths.isEmpty()) |
| 81 | + base = filePaths.at(0).filename(); |
| 82 | + } |
| 83 | + if (base.isEmpty()) |
| 84 | + base = u"Torrent"_s; |
| 85 | + |
| 86 | + // Avoid "Name [qb-… ] [qb-…]" if the display/root name already carries this tag. |
| 87 | + if (base.endsWith(tag)) |
| 88 | + base.chop(tag.size()); |
| 89 | + |
| 90 | + return base; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +QString BitTorrent::payloadHashTag(const TorrentID &id) |
| 95 | +{ |
| 96 | + return u" [qb-"_s + id.toString().left(HASH_TAG_HEX_LEN) + u']'; |
| 97 | +} |
| 98 | + |
| 99 | +QString BitTorrent::payloadHashDirectoryName(const TorrentID &id, const QString &originalName) |
| 100 | +{ |
| 101 | + return hashedComponent(originalName, payloadHashTag(id)); |
| 102 | +} |
| 103 | + |
| 104 | +PathList BitTorrent::applyPayloadHashNaming(PathList filePaths, const TorrentID &id, const QString &torrentName) |
| 105 | +{ |
| 106 | + if (filePaths.isEmpty()) |
| 107 | + return filePaths; |
| 108 | + |
| 109 | + const QString tag = payloadHashTag(id); |
| 110 | + const QString hashDir = payloadHashDirectoryName(id, originalNameForHashDir(filePaths, torrentName, tag)); |
| 111 | + |
| 112 | + const Path rootFolder = Path::findRootFolder(filePaths); |
| 113 | + if (!rootFolder.isEmpty()) |
| 114 | + { |
| 115 | + // Already under the correct hash directory. |
| 116 | + if (rootFolder.toString() == hashDir) |
| 117 | + return filePaths; |
| 118 | + // Rename existing top-level folder (e.g. Show/ → Show [qb-HASH]/). |
| 119 | + return renameRootFolder(std::move(filePaths), rootFolder.toString(), hashDir); |
| 120 | + } |
| 121 | + |
| 122 | + // Single file or rootless multi-file: wrap under the hash directory. |
| 123 | + Path::addRootFolder(filePaths, Path(hashDir)); |
| 124 | + return filePaths; |
| 125 | +} |
0 commit comments