| 109 | BaseEntityLoadTask::BaseEntityLoadTask(BaseEntity* parent, Net::Mode mode) : m_entity(parent), m_mode(mode) {} |
| 110 | |
| 111 | void BaseEntityLoadTask::executeTask() |
| 112 | { |
| 113 | const QString fname = QDir("meta").absoluteFilePath(m_entity->localFilename()); |
| 114 | auto hashMatches = false; |
| 115 | // the file exists on disk try to load it |
| 116 | if (QFile::exists(fname)) { |
| 117 | try { |
| 118 | QByteArray fileData; |
| 119 | // read local file if nothing is loaded yet |
| 120 | if (m_entity->m_load_status == BaseEntity::LoadStatus::NotLoaded || m_entity->m_file_sha256.isEmpty()) { |
| 121 | setStatus(tr("Loading local file")); |
| 122 | fileData = FS::read(fname); |
| 123 | m_entity->m_file_sha256 = Hashing::hash(fileData, Hashing::Algorithm::Sha256); |
| 124 | } |
| 125 | |
| 126 | // on online the hash needs to match |
| 127 | hashMatches = m_entity->m_sha256 == m_entity->m_file_sha256; |
| 128 | if (m_mode == Net::Mode::Online && !m_entity->m_sha256.isEmpty() && !hashMatches) { |
| 129 | throw Exception("mismatched checksum"); |
| 130 | } |
| 131 | |
| 132 | // load local file |
| 133 | if (m_entity->m_load_status == BaseEntity::LoadStatus::NotLoaded) { |
| 134 | auto doc = Json::requireDocument(fileData, fname); |
| 135 | auto obj = Json::requireObject(doc, fname); |
| 136 | m_entity->parse(obj); |
| 137 | m_entity->m_load_status = BaseEntity::LoadStatus::Local; |
| 138 | } |
| 139 | |
| 140 | } catch (const Exception& e) { |
| 141 | qDebug() << QString("Unable to parse file %1: %2").arg(fname, e.cause()); |
| 142 | // just make sure it's gone and we never consider it again. |
| 143 | FS::deletePath(fname); |
| 144 | m_entity->m_load_status = BaseEntity::LoadStatus::NotLoaded; |
| 145 | } |
| 146 | } |
| 147 | // if we need remote update, run the update task |
| 148 | auto wasLoadedOffline = m_entity->m_load_status != BaseEntity::LoadStatus::NotLoaded && m_mode == Net::Mode::Offline; |
| 149 | // if has is not present allways fetch from remote(e.g. the main index file), else only fetch if hash doesn't match |
| 150 | auto wasLoadedRemote = m_entity->m_sha256.isEmpty() ? m_entity->m_load_status == BaseEntity::LoadStatus::Remote : hashMatches; |
| 151 | if (wasLoadedOffline || wasLoadedRemote) { |
| 152 | emitSucceeded(); |
| 153 | return; |
| 154 | } |
| 155 | m_task.reset(new NetJob(QObject::tr("Download of meta file %1").arg(m_entity->localFilename()), APPLICATION->network())); |
| 156 | auto url = m_entity->url(); |
| 157 | auto entry = APPLICATION->metacache()->resolveEntry("meta", m_entity->localFilename()); |
| 158 | entry->setStale(true); |
| 159 | auto dl = Net::ApiDownload::makeCached(url, entry); |
| 160 | /* |
| 161 | * The validator parses the file and loads it into the object. |
| 162 | * If that fails, the file is not written to storage. |
| 163 | */ |
| 164 | if (!m_entity->m_sha256.isEmpty()) |
| 165 | dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Algorithm::Sha256, m_entity->m_sha256)); |
| 166 | dl->addValidator(new ParsingValidator(m_entity)); |
| 167 | m_task->addNetAction(dl); |
| 168 | m_task->setAskRetry(false); |
nothing calls this directly
no test coverage detected