public Task <bool> FileExists(string filespaceId, string filename) { if (TCCFile.FileCacheable(filename)) { if (fileCache.TryGetValue(filename, out _)) { return(Task.FromResult(true)); } } return(PathExists(filespaceId, filename)); }
private async Task <Stream> GetFileEx(string filespaceId, string fullName, int retries) { Log.LogDebug($"{nameof(GetFileEx)}: filespaceId={filespaceId}, fullName={fullName}, retries={retries}"); byte[] file; var cacheable = TCCFile.FileCacheable(fullName); if (cacheable) { Log.LogDebug("Trying to extract from cache {0} with cache size {1}", fullName, fileCache.Count); if (fileCache.TryGetValue(fullName, out file)) { Log.LogDebug("Serving TCC tile request from cache {0}", fullName); if (file.Length == 0) { Log.LogDebug("Serving TCC tile request from cache empty tile"); return(null); } return(new MemoryStream(file)); } } var getFileParams = new GetFileParams { filespaceid = filespaceId, path = fullName }; if (string.IsNullOrEmpty(tccBaseUrl)) { throw new Exception("Configuration Error - no TCC url specified"); } var gracefulClient = new GracefulWebRequest(logFactory, configStore, _httpClientFactory); var(requestString, headers) = FormRequest(getFileParams, "GetFile"); try { if (!cacheable) { using (var responseStream = await(await gracefulClient.ExecuteRequestAsStreamContent(requestString, HttpMethod.Get, headers, retries: 0)).ReadAsStreamAsync()) { responseStream.Position = 0; file = new byte[responseStream.Length]; responseStream.Read(file, 0, file.Length); return(new MemoryStream(file)); } } using (var responseStream = await(await gracefulClient.ExecuteRequestAsStreamContent(requestString, HttpMethod.Get, headers, retries: retries)).ReadAsStreamAsync()) { Log.LogDebug("Adding TCC tile request to cache {0}", fullName); responseStream.Position = 0; file = new byte[responseStream.Length]; responseStream.Read(file, 0, file.Length); fileCache.Set(fullName, file, DateTimeOffset.MaxValue); Log.LogDebug("About to extract file name for {0}", fullName); var baseFileName = TCCFile.ExtractFileNameFromTileFullName(fullName); Log.LogDebug("Extracted file name is {0}", baseFileName); cacheLookup.AddFile(baseFileName, fullName); return(new MemoryStream(file)); } } catch (WebException webException) { using (var response = webException.Response) { Log.LogWarning( $"Can not execute request TCC request with error {webException.Status} and {webException.Message}. {GetStringFromResponseStream(response)}"); } //let's cache the response anyway but for a limited time fileCache.Set(fullName, new byte[0], DateTimeOffset.UtcNow.AddHours(12)); } catch (Exception e) { Log.LogWarning(e, "Can not execute request TCC response."); } return(null); }