public IObservable <Stream> SafeOpenFileAsync(string path, FileMode mode, FileAccess access, FileShare share, IScheduler scheduler)
 {
     if (mode == FileMode.Create ||
         mode == FileMode.CreateNew ||
         (mode == FileMode.OpenOrCreate && !File.Exists(path)))
     {
         return(Observable.Throw <Stream>(new Exception("Read only mode")));
     }
     return(_inner.SafeOpenFileAsync(path, mode, FileAccess.Read, share, scheduler));
 }
Example #2
0
        AsyncSubject <byte[]> FetchOrWriteBlobFromDisk(string key, object byteData, bool synchronous)
        {
            // If this is secretly a write, dispatch to WriteBlobToDisk (we're
            // kind of abusing the 'context' variable from MemoizingMRUCache
            // here a bit)
            if (byteData != null)
            {
                return(WriteBlobToDisk(key, (byte[])byteData, synchronous));
            }

            var ret = new AsyncSubject <byte[]>();
            var ms  = new MemoryStream();

            var scheduler = synchronous ? System.Reactive.Concurrency.Scheduler.Immediate : Scheduler;

            if (disposed)
            {
                Observable.Throw <byte[]>(new ObjectDisposedException("PersistentBlobCache"))
                .Multicast(ret)
                .PermaRef();
                return(ret);
            }

            Func <IObservable <byte[]> > readResult = () =>
                                                      Observable.Defer(() =>
                                                                       filesystem.SafeOpenFileAsync(GetPathForKey(key), FileMode.Open, FileAccess.Read, FileShare.Read, scheduler))
                                                      .Retry(1)
                                                      .SelectMany(x => x.CopyToAsync(ms, scheduler))
                                                      .SelectMany(x => AfterReadFromDiskFilter(ms.ToArray(), scheduler))
                                                      .Catch <byte[], FileNotFoundException>(ex => ObservableThrowKeyNotFoundException(key, ex))
                                                      .Catch <byte[], IsolatedStorageException>(ex => ObservableThrowKeyNotFoundException(key, ex))
                                                      .Do(_ =>
            {
                if (!synchronous && key != BlobCacheIndexKey)
                {
                    actionTaken.OnNext(Unit.Default);
                }
            });

            readResult().Multicast(ret).PermaRef();
            return(ret);
        }