void AssetUpload::start() { if (QThread::currentThread() != thread()) { QMetaObject::invokeMethod(this, "start"); return; } if (_data.isEmpty() && !_filename.isEmpty()) { // try to open the file at the given filename QFile file { _filename }; if (file.open(QIODevice::ReadOnly)) { _data = file.readAll(); } else { // we couldn't open the file - set the error result _error = FileOpenError; // emit that we are done emit finished(this, QString()); return; } } // ask the AssetClient to upload the asset and emit the proper signals from the passed callback auto assetClient = DependencyManager::get<AssetClient>(); if (!_filename.isEmpty()) { qCDebug(asset_client) << "Attempting to upload" << _filename << "to asset-server."; } assetClient->uploadAsset(_data, [this](bool responseReceived, AssetServerError error, const QString& hash){ if (!responseReceived) { _error = NetworkError; } else { switch (error) { case AssetServerError::NoError: _error = NoError; break; case AssetServerError::AssetTooLarge: _error = TooLarge; break; case AssetServerError::PermissionDenied: _error = PermissionDenied; break; case AssetServerError::FileOperationFailed: _error = ServerFileError; break; default: _error = FileOpenError; break; } } if (_error == NoError && hash == hashData(_data).toHex()) { saveToCache(getATPUrl(hash), _data); } emit finished(this, hash); }); }
void ModelManager::loadList() { fs::path ModelPath(L"./Data/Models/"), CacheFilePath(L"./Data/ModelCache.dat"); if (!fs::exists(ModelPath)) throw std::ios_base::failure("Model directory not found"); if (loadFromCache(CacheFilePath, ModelPath)) return; // Rebuild the cache file, since it is invalid loadInternal(ModelPath); saveToCache(CacheFilePath, ModelPath); }
virtual ~DataQueueSerializable() { saveToCache(); }
void AssetRequest::start() { if (QThread::currentThread() != thread()) { QMetaObject::invokeMethod(this, "start", Qt::AutoConnection); return; } if (_state != NotStarted) { qCWarning(asset_client) << "AssetRequest already started."; return; } // in case we haven't parsed a valid hash, return an error now if (!isValidHash(_hash)) { _error = InvalidHash; _state = Finished; emit finished(this); return; } // Try to load from cache _data = loadFromCache(getUrl()); if (!_data.isNull()) { _error = NoError; _loadedFromCache = true; _state = Finished; emit finished(this); return; } _state = WaitingForData; auto assetClient = DependencyManager::get<AssetClient>(); auto that = QPointer<AssetRequest>(this); // Used to track the request's lifetime auto hash = _hash; _assetRequestID = assetClient->getAsset(_hash, _byteRange.fromInclusive, _byteRange.toExclusive, [this, that, hash](bool responseReceived, AssetServerError serverError, const QByteArray& data) { if (!that) { qCWarning(asset_client) << "Got reply for dead asset request " << hash << "- error code" << _error; // If the request is dead, return return; } _assetRequestID = INVALID_MESSAGE_ID; if (!responseReceived) { _error = NetworkError; } else if (serverError != AssetServerError::NoError) { switch (serverError) { case AssetServerError::AssetNotFound: _error = NotFound; break; case AssetServerError::InvalidByteRange: _error = InvalidByteRange; break; default: _error = UnknownError; break; } } else { if (!_byteRange.isSet() && hashData(data).toHex() != _hash) { // the hash of the received data does not match what we expect, so we return an error _error = HashVerificationFailed; } if (_error == NoError) { _data = data; _totalReceived += data.size(); emit progress(_totalReceived, data.size()); if (!_byteRange.isSet()) { saveToCache(getUrl(), data); } } } if (_error != NoError) { qCWarning(asset_client) << "Got error retrieving asset" << _hash << "- error code" << _error; } _state = Finished; emit finished(this); }, [this, that](qint64 totalReceived, qint64 total) { if (!that) { // If the request is dead, return return; } emit progress(totalReceived, total); }); }
// ------------------------------------------------------------- bool Webpage::open(string url, bool wellFormed, bool useCache) { if(Webpage::cacheDirectory.empty()) { useCache=false; } bool loaded=false; if(useCache) { locale loc; const collate<char>& coll = use_facet<collate<char> >(loc); long myhash = coll.hash(url.data(),url.data()+url.length()); sprintf(cachefile, "%s/%ld.cache", Webpage::cacheDirectory.c_str(), myhash); loaded = loadFromCache(); } if(!loaded) { contents = download(url, verbose); if(contents.empty()) { if(verbose) cerr << "No contents downloaded." << endl; return false; } if(!wellFormed) { tidy_me(); } } // get rid of doctype line. It messes up the parser string prefix = "<!DOCTYPE"; if(contents.compare(0, prefix.size(), prefix)==0) { size_t pos = contents.find(">"); contents.erase(0, pos+1); } // HACK: I think tidySaveString puts some extra junk at the end of the document // if there is anything after </html>, get rid of it. size_t end = contents.find("</html>"); if(end!=string::npos) { contents.erase(end+7); } if(useCache) { saveToCache(); } if(verbose) cerr << "Parsing document. Length: " << contents.length() << endl; doc = xmlParseMemory(contents.c_str(), contents.length()); if (doc == NULL) { if(verbose) cerr << "ERROR: Unable to parse HTML" << endl; return false; } xpathCtx = xmlXPathNewContext(doc); if(xpathCtx == NULL) { xmlFreeDoc(doc); if(verbose) cerr << "ERROR: Unable to create new XPath context" << endl; return false; } return true; }