|
| 1 | +#include "vxurlutils.h" |
| 2 | + |
| 3 | +#include <QFile> |
| 4 | +#include <QDir> |
| 5 | +#include <QDirIterator> |
| 6 | +#include <QTemporaryFile> |
| 7 | + |
| 8 | +#include <core/exception.h> |
| 9 | +#include <core/global.h> |
| 10 | + |
| 11 | +#include "pathutils.h" |
| 12 | +#include <QJsonArray> |
| 13 | +#include <QJsonDocument> |
| 14 | +#include <QJsonObject> |
| 15 | +#include <QJsonParseError> |
| 16 | +#include <QJsonValue> |
| 17 | + |
| 18 | +using namespace vnotex; |
| 19 | + |
| 20 | +QString VxUrlUtils::generateVxURL(const QString &p_signature, const QString &p_filePath) |
| 21 | +{ |
| 22 | + return QString("#%1:%2").arg(p_signature, p_filePath); |
| 23 | +} |
| 24 | + |
| 25 | +QString VxUrlUtils::getSignatureFromVxURL(const QString &p_vxUrl) |
| 26 | +{ |
| 27 | + QString signature = p_vxUrl; |
| 28 | + // check if file is "#signature:fileFullName" |
| 29 | + if (signature.startsWith('#')) { |
| 30 | + signature = signature.mid(1); // remove '#' |
| 31 | + if (signature.contains(':')) { |
| 32 | + signature = signature.split(':').first(); // get 'signature' |
| 33 | + return signature; |
| 34 | + } |
| 35 | + } // if not 'signature', return original 'vxUrl' |
| 36 | + return p_vxUrl; |
| 37 | +} |
| 38 | + |
| 39 | +QString VxUrlUtils::getFilePathFromVxURL(const QString &p_vxUrl) { |
| 40 | + QString filePath = p_vxUrl; |
| 41 | + // check if file is "#signature:fileFullName" |
| 42 | + if (p_vxUrl.startsWith('#')) { |
| 43 | + int colonPos = p_vxUrl.indexOf(':'); |
| 44 | + if (colonPos != -1) { |
| 45 | + filePath = p_vxUrl.mid(colonPos + 1); |
| 46 | + filePath = PathUtils::fileName(filePath); // get 'filePath' |
| 47 | + return filePath; |
| 48 | + } |
| 49 | + } // if not 'filePath', return original 'vxUrl' |
| 50 | + return p_vxUrl; |
| 51 | +} |
| 52 | + |
| 53 | +QString VxUrlUtils::getSignatureFromFilePath(const QString &p_filePath) |
| 54 | +{ |
| 55 | + QFileInfo fileInfo(p_filePath); |
| 56 | + QString vxJsonPath; |
| 57 | + QString currentFileName; |
| 58 | + // get file's signature from vx.json |
| 59 | + if (fileInfo.isFile()) { |
| 60 | + QString dirPath = PathUtils::parentDirPath(p_filePath); |
| 61 | + vxJsonPath = PathUtils::concatenateFilePath(dirPath, "vx.json"); |
| 62 | + currentFileName = PathUtils::fileName(p_filePath); |
| 63 | + } else if (fileInfo.isDir()) { |
| 64 | + vxJsonPath = PathUtils::concatenateFilePath(p_filePath, "vx.json"); |
| 65 | + currentFileName = PathUtils::fileName(p_filePath); |
| 66 | + } else { |
| 67 | + return QString(); |
| 68 | + } |
| 69 | + |
| 70 | + QFile vxFile(vxJsonPath); |
| 71 | + if (!vxFile.open(QIODevice::ReadOnly)) { |
| 72 | + return QString(); |
| 73 | + } |
| 74 | + |
| 75 | + QByteArray data = vxFile.readAll(); |
| 76 | + vxFile.close(); |
| 77 | + |
| 78 | + QJsonParseError parseError; |
| 79 | + QJsonDocument doc = QJsonDocument::fromJson(data, &parseError); |
| 80 | + if (parseError.error != QJsonParseError::NoError) { |
| 81 | + return QString(); |
| 82 | + } |
| 83 | + |
| 84 | + QJsonObject obj = doc.object(); |
| 85 | + QString signature; |
| 86 | + |
| 87 | + if (obj.contains("files") && obj["files"].isArray()) { |
| 88 | + QJsonArray filesArray = obj["files"].toArray(); |
| 89 | + for (const QJsonValue &fileVal : filesArray) { |
| 90 | + QJsonObject fileObj = fileVal.toObject(); |
| 91 | + if (fileObj["name"].toString() == currentFileName) { |
| 92 | + signature = fileObj["signature"].toString(); |
| 93 | + return signature; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (signature.isEmpty() && obj.contains("signature")) { |
| 99 | + signature = obj["signature"].toString(); |
| 100 | + } |
| 101 | + |
| 102 | + return signature; |
| 103 | +} |
| 104 | + |
| 105 | +QString VxUrlUtils::getFilePathFromSignature(const QString &p_startPath, const QString &p_signature) |
| 106 | +{ |
| 107 | + // Find the file with the specified signature in all vx.json files under the specified directory |
| 108 | + QDirIterator it(p_startPath, {"vx.json"}, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); |
| 109 | + |
| 110 | + while (it.hasNext()) { |
| 111 | + const QString vxPath = it.next(); |
| 112 | + QFile vxFile(vxPath); |
| 113 | + if (!vxFile.open(QIODevice::ReadOnly)) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + const QByteArray data = vxFile.readAll(); |
| 118 | + vxFile.close(); |
| 119 | + |
| 120 | + QJsonParseError parseError; |
| 121 | + const QJsonDocument doc = QJsonDocument::fromJson(data, &parseError); |
| 122 | + if (parseError.error != QJsonParseError::NoError) { |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + const QJsonObject json = doc.object(); |
| 127 | + QString signature; |
| 128 | + QString fileName; |
| 129 | + |
| 130 | + // Find signature in files array |
| 131 | + const auto filesArray = json.value("files").toArray(); |
| 132 | + for (const auto &fileItem : filesArray) { |
| 133 | + const auto fileObj = fileItem.toObject(); |
| 134 | + if (fileObj["signature"].toString() == p_signature) { |
| 135 | + fileName = fileObj["name"].toString(); |
| 136 | + signature = p_signature; |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + // If not found in files array, use directory signature |
| 141 | + if (signature.isEmpty()) { |
| 142 | + signature = json.value("signature").toString(); |
| 143 | + } |
| 144 | + |
| 145 | + if (!signature.isEmpty() && signature == p_signature) { |
| 146 | + const QString dirPath = QFileInfo(vxPath).absolutePath(); |
| 147 | + const QString fullPath = PathUtils::concatenateFilePath(dirPath, fileName); |
| 148 | + return fullPath; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return QString(); |
| 153 | +} |
0 commit comments