/// <summary> /// Tries to get cached file as byte array. /// </summary> /// <returns>The get async.</returns> /// <param name="key">Key.</param> /// <param name="token">Token.</param> public async Task <byte[]> TryGetAsync(string key, CancellationToken token) { key = SanitizeKey(key); await WaitForPendingWriteIfExists(key).ConfigureAwait(false); if (!entries.ContainsKey(key)) { return(null); } try { string filepath = Path.Combine(basePath, key); if (!FileStore.Exists(filepath)) { return(null); } return(await FileStore.ReadBytesAsync(filepath, token).ConfigureAwait(false)); } catch { return(null); } }
/// <summary> /// Tries to get cached file as byte array. /// </summary> /// <returns>The get async.</returns> /// <param name="key">Key.</param> /// <param name="token">Token.</param> public async Task <byte[]> TryGetAsync(string key, CancellationToken token) { key = SanitizeKey(key); await WaitForPendingWriteIfExists(key).ConfigureAwait(false); if (!entries.ContainsKey(key)) { return(null); } try { string filepath = Path.Combine(basePath, key); if (!FileStore.Exists(filepath)) { return(null); } using (var fs = FileStore.GetInputStream(filepath)) { using (var ms = new MemoryStream()) { await fs.CopyToAsync(ms, BufferSize, token).ConfigureAwait(false); return(ms.ToArray()); } } } catch { return(null); } }
/// <summary> /// Tries to get cached file as byte array. /// </summary> /// <returns>The get async.</returns> /// <param name="key">Key.</param> /// <param name="token">Token.</param> public async Task <byte[]> TryGetAsync(string key, CancellationToken token) { var sanitizedKey = SanitizeKey(key); await WaitForPendingWriteIfExists(sanitizedKey).ConfigureAwait(false); try { CacheEntry entry; if (!_entries.TryGetValue(sanitizedKey, out entry)) { return(null); } string filepath = Path.Combine(_cachePath, entry.FileName); if (!FileStore.Exists(filepath)) { return(null); } return(await FileStore.ReadBytesAsync(filepath, token).ConfigureAwait(false)); } catch { return(null); } }
public Task <UIImageData> GetData(string identifier, CancellationToken token) { int scale = (int)ScaleHelper.Scale; if (scale > 1) { var filename = Path.GetFileNameWithoutExtension(identifier); var extension = Path.GetExtension(identifier); const string pattern = "{0}@{1}x{2}"; while (scale > 1) { var file = String.Format(pattern, filename, scale, extension); if (FileStore.Exists(file)) { return(GetDataInternal(file, token)); } scale--; } } if (FileStore.Exists(identifier)) { return(GetDataInternal(identifier, token)); } return(Task.FromResult((UIImageData)null)); }
public virtual Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token) { string file = null; int scale = (int)ScaleHelper.Scale; if (scale > 1) { var filename = Path.GetFileNameWithoutExtension(identifier); var extension = Path.GetExtension(identifier); const string pattern = "{0}@{1}x{2}"; while (scale > 1) { token.ThrowIfCancellationRequested(); var tmpFile = string.Format(pattern, filename, scale, extension); if (FileStore.Exists(tmpFile)) { file = tmpFile; break; } scale--; } } if (string.IsNullOrEmpty(file) && FileStore.Exists(identifier)) { file = identifier; } token.ThrowIfCancellationRequested(); if (!string.IsNullOrEmpty(file)) { var stream = FileStore.GetInputStream(file, true); var imageInformation = new ImageInformation(); imageInformation.SetPath(identifier); imageInformation.SetFilePath(file); var result = (LoadingResult)(int)parameters.Source; if (parameters.LoadingPlaceholderPath == identifier) { result = (LoadingResult)(int)parameters.LoadingPlaceholderSource; } else if (parameters.ErrorPlaceholderPath == identifier) { result = (LoadingResult)(int)parameters.ErrorPlaceholderSource; } return(Task.FromResult(new Tuple <Stream, LoadingResult, ImageInformation>( stream, result, imageInformation))); } throw new FileNotFoundException(identifier); }
public Task <WithLoadingResult <Stream> > GetStream(string identifier, CancellationToken token) { if (!FileStore.Exists(identifier)) { return(Task.FromResult(WithLoadingResult.Encapsulate((Stream)null, LoadingResult.NotFound))); } var result = WithLoadingResult.Encapsulate(FileStore.GetInputStream(identifier), LoadingResult.Disk); return(Task.FromResult(result)); }
public Task <WithLoadingResult <Stream> > GetStream(string identifier, CancellationToken token) { if (!FileStore.Exists(identifier)) { return(Task.FromResult(WithLoadingResult.Encapsulate((Stream)null, LoadingResult.NotFound))); } var imageInformation = new ImageInformation(); imageInformation.SetPath(identifier); imageInformation.SetFilePath(identifier); var result = WithLoadingResult.Encapsulate(FileStore.GetInputStream(identifier), LoadingResult.Disk, imageInformation); return(Task.FromResult(result)); }
public string Read(int id) { log.Reading(id); var path = GetFileName(id); if (!fileStore.Exists(path)) { log.DidNotFind(id); return(string.Empty); } var message = fileStore.ReadAllText(path); log.Returning(id); return(message); }
public async Task <UIImageData> GetData(string identifier, CancellationToken token) { if (!FileStore.Exists(identifier)) { return(null); } var bytes = await FileStore.ReadBytesAsync(identifier).ConfigureAwait(false); var result = (LoadingResult)(int)_source; // Some values of ImageSource and LoadingResult are shared return(new UIImageData() { Data = bytes, Result = result, ResultIdentifier = identifier }); }
public Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token) { if (!FileStore.Exists(identifier)) { throw new FileNotFoundException(identifier); } var stream = FileStore.GetInputStream(identifier, true); var imageInformation = new ImageInformation(); imageInformation.SetPath(identifier); imageInformation.SetFilePath(identifier); return(Task.FromResult(new Tuple <Stream, LoadingResult, ImageInformation>( stream, LoadingResult.Disk, imageInformation))); }
public virtual Task <DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token) { if (!FileStore.Exists(identifier)) { throw new FileNotFoundException(identifier); } token.ThrowIfCancellationRequested(); var stream = FileStore.GetInputStream(identifier, true); var imageInformation = new ImageInformation(); imageInformation.SetPath(identifier); imageInformation.SetFilePath(identifier); return(Task.FromResult(new DataResolverResult( stream, LoadingResult.Disk, imageInformation))); }
/// <summary> /// Ensures the file can actually be written to the path and will return the full path as string /// </summary> /// <param name="folder">The name of the folder</param> /// <param name="fullName">The name of the file</param> /// <returns>Full path of the file location</returns> private string FileChecks(string folder, string fullName) { try { FileStore.EnsureFolderExists(folder); } catch (Exception ex) { Mvx.IoCProvider.Resolve <IMvxLog>().ErrorException("ExStorageService.FileChecks", ex); } var fullPath = FileStore.PathCombine(folder, fullName); if (FileStore.Exists(fullPath)) { DeleteFile(folder, fullName); } return(fullPath); }
public bool TryGetValue <T>(object key, out T value) { if (!_memCache.TryGetValue(key, out value)) // check inside memory { if (_fileStore.Exists(key, cacheCluster)) // check inside disk { // check expiration var cacheEntryOptions = _fileStore.LoadAttachment <FileCacheEntryOptions>(key, "FsCacheEntryOptions", cacheCluster); var expired = false; if (cacheEntryOptions != null) { expired = cacheEntryOptions.CheckExpired(_systemClock.UtcNow); } if (!expired) { value = _fileStore.Load <T>(key, cacheCluster); // load from disk // create cache in-memory var entry = CreateEntry(key); if (cacheEntryOptions != null) { entry.SetOptions(cacheEntryOptions.ToMemoryOptions()); } entry.SetValue(value); // need to manually call dispose instead of having a using // in case the factory passed in throws, in which case we // do not want to add the entry to the cache entry.Dispose(); } return(!expired); } } else { return(true); } return(false); }
public async Task <byte[]> TryGetAsync(string key) { key = SanitizeKey(key); byte[] data = null; if (!entries.ContainsKey(key)) { return(null); } try { string filepath = Path.Combine(basePath, key); if (!FileStore.Exists(filepath)) { return(null); } data = await FileStore.ReadBytesAsync(filepath).ConfigureAwait(false); } catch { return(null); } return(data); }
/// <summary> /// Check if a file exists on a certain location /// </summary> /// <param name="folder">The name of the folder</param> /// <param name="fullName">The name of the file</param> /// <returns>True if the file exists, otherwise false.</returns> public override bool Exists(string folder, string fullName) { var fullPath = FileStore.PathCombine(folder, fullName); return(FileStore.Exists(fullPath)); }
protected virtual async Task <UIImage> GetImageAsync(string sourcePath, ImageSource source) { if (CancellationToken.IsCancellationRequested) { return(null); } byte[] bytes = null; string path = sourcePath; try { switch (source) { case ImageSource.ApplicationBundle: case ImageSource.Filepath: if (FileStore.Exists(path)) { bytes = await FileStore.ReadBytesAsync(path).ConfigureAwait(false); } break; case ImageSource.Url: var downloadedData = await DownloadCache.GetAsync(path, Parameters.CacheDuration).ConfigureAwait(false); bytes = downloadedData.Bytes; path = downloadedData.CachedPath; break; } } catch (Exception ex) { Logger.Error("Unable to retrieve image data", ex); Parameters.OnError(ex); return(null); } if (bytes == null) { return(null); } return(await Task.Run(() => { if (CancellationToken.IsCancellationRequested) { return null; } // Special case to handle WebP decoding on iOS if (sourcePath.ToLowerInvariant().EndsWith(".webp", StringComparison.InvariantCulture)) { return new WebP.Touch.WebPCodec().Decode(bytes); } nfloat scale = _imageScale >= 1 ? _imageScale : _screenScale; var image = new UIImage(NSData.FromArray(bytes), scale); if (Parameters.Transformations != null && Parameters.Transformations.Count > 0) { foreach (var transformation in Parameters.Transformations) { try { var bitmapHolder = transformation.Transform(new BitmapHolder(image)); image = bitmapHolder.ToNative(); } catch (Exception ex) { Logger.Error("Can't apply transformation " + transformation.Key + " to image " + path, ex); } } } return image; }).ConfigureAwait(false)); }