Пример #1
0
        public Task <Stream> DownloadFile(FileHeader file)
        {
            var name          = file.FullPath;
            var readingStream = StorageStream.Reading(filesystem.Storage, name);

            return(new CompletedTask <Stream>(readingStream));
        }
Пример #2
0
        public MultipartSyncStreamProvider(SynchronizingFileStream synchronizingFile, StorageStream localFile)
        {
            this.synchronizingFile = synchronizingFile;
            this.localFile         = localFile;

            BytesTransfered = BytesCopied = NumberOfFileParts = 0;
        }
Пример #3
0
        public HttpResponseMessage Get(string name)
        {
            name = RavenFileNameHelper.RavenPath(name);
            FileAndPagesInformation fileAndPages = null;

            try
            {
                Storage.Batch(accessor => fileAndPages = accessor.GetFile(name, 0, 0));
            }
            catch (FileNotFoundException)
            {
                log.Debug("File '{0}' was not found", name);
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            if (fileAndPages.Metadata.Keys.Contains(SynchronizationConstants.RavenDeleteMarker))
            {
                log.Debug("File '{0}' is not accessible to get (Raven-Delete-Marker set)", name);
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var readingStream = StorageStream.Reading(Storage, name);
            var result        = StreamResult(name, readingStream);

            var etag = new Etag(fileAndPages.Metadata.Value <string>(Constants.MetadataEtagField));

            fileAndPages.Metadata.Remove(Constants.MetadataEtagField);
            WriteHeaders(fileAndPages.Metadata, etag, result);

            return(result.WithNoCache());
        }
Пример #4
0
        public HttpResponseMessage Get(string name)
        {
            name = FileHeader.Canonize(name);
            FileAndPagesInformation fileAndPages = null;

            Storage.Batch(accessor => fileAndPages = accessor.GetFile(name, 0, 0));

            if (fileAndPages.Metadata.Keys.Contains(SynchronizationConstants.RavenDeleteMarker))
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("File '{0}' is not accessible to get (Raven-Delete-Marker set)", name);
                }
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var readingStream = StorageStream.Reading(Storage, name);

            var filename = GetFileName(name, fileAndPages.Metadata);
            var result   = StreamResult(filename, readingStream);

            var etag = new Etag(fileAndPages.Metadata.Value <string>(Constants.MetadataEtagField));

            fileAndPages.Metadata.Remove(Constants.MetadataEtagField);
            WriteHeaders(fileAndPages.Metadata, etag, result);

            if (log.IsDebugEnabled)
            {
                log.Debug("File '{0}' with etag {1} is being retrieved.", name, etag);
            }

            return(result.WithNoCache());
        }
Пример #5
0
        public void TestStreamToTextOtherStream()
        {
            var stream1 = new StorageStream(new MemoryStorage());

            stream1.Write(Encoding.Default.GetBytes("Hello world"), 0, 11);
            stream1.Seek(0, SeekOrigin.Begin);
            Assert.AreEqual("Hello world", stream1.ToText());
        }
Пример #6
0
        private void StreamExportToClient(Stream gzip2, string[] fileNames)
        {
            using (var gzip = new BufferedStream(gzip2))
            //     using (var gzip = new GZipStream(stream, CompressionMode.Compress,true))
            {
                var binaryWriter = new BinaryWriter(gzip);

                var buffer     = new byte[StorageConstants.MaxPageSize];
                var pageBuffer = new byte[StorageConstants.MaxPageSize];

                foreach (var name in fileNames)
                {
                    FileAndPagesInformation fileAndPages = null;
                    var cannonizedName = FileHeader.Canonize(name);

                    try
                    {
                        Storage.Batch(accessor => fileAndPages = accessor.GetFile(cannonizedName, 0, 0));
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }

                    // if we didn't find the document, we'll write "-1" to the stream, signaling that
                    if (fileAndPages.Metadata.Keys.Contains(SynchronizationConstants.RavenDeleteMarker))
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("File '{0}' is not accessible to get (Raven-Delete-Marker set)", name);
                        }

                        binaryWriter.Write(-1);
                        continue;
                    }

                    var fileSize = fileAndPages.UploadedSize;
                    binaryWriter.Write(fileSize);

                    var readingStream = StorageStream.Reading(Storage, cannonizedName);
                    var bytesRead     = 0;
                    do
                    {
                        try
                        {
                            bytesRead = readingStream.ReadUsingExternalTempBuffer(buffer, 0, buffer.Length, pageBuffer);
                        }
                        catch (Exception ex)
                        {
                            throw;
                        }
                        gzip.Write(buffer, 0, bytesRead);
                    } while (bytesRead > 0);
                }
            }

            //gzip2.Flush();
        }
Пример #7
0
        private void AddImages(IEnumerable <string> files)
        {
            if (files.Count() > 0)
            {
                if (items == null)
                {
                    items = new List <ImageInfo>();
                }

                //IsEnabled = false;
                //ProgressDialog dialog = new ProgressDialog("Progress", "Adding files...");
                //dialog.Show();
                try
                {
                    int count = files.Count();
                    int index = 1;

                    storage.StartTransaction();
                    foreach (string file in files)
                    {
                        using (StorageStream s = storage.CreateStream(Guid.NewGuid()))
                        {
                            using (FileStream fs = File.OpenRead(file))
                            {
                                byte[] buf = new byte[65536];

                                int l = fs.Read(buf, 0, buf.Length);
                                while (l > 0)
                                {
                                    s.Write(buf, 0, l);
                                    l = fs.Read(buf, 0, buf.Length);
                                }
                            }

                            items.Add(new ImageInfo()
                            {
                                Name     = System.IO.Path.GetFileName(file),
                                Size     = (int)s.Length,
                                StreamId = s.StreamId
                            });
                        }
                        //dialog.Progress = (double)index * 100d / (double)count;
                        index++;
                    }

                    SaveImageList();
                    UpdateUI();
                    RefreshMap(imageList.SelectedValue as Guid?);

                    storage.CommitTransaction();
                }
                finally
                {
                    //dialog.Close();
                    //IsEnabled = true;
                }
            }
        }
Пример #8
0
        public void TestCopyNullArg()
        {
            Assert.IsNotNull(stream);

            StorageStream streamCopy = null;

            // set to a non zero position to make sure the copy is reset back to 0
            stream.CopyTo(streamCopy);
        }
Пример #9
0
        public void TestCopy()
        {
            Assert.IsNotNull(stream);
            StorageStream streamCopy = new StorageStream();

            // set to a non zero position to make sure the copy is reset back to 0
            stream.Position = 2;
            stream.CopyTo(streamCopy);

            Assert.IsNotNull(streamCopy);
            Assert.Equals(stream, streamCopy);
            Assert.IsTrue(streamCopy.Position == 0, "StorageStreamTest: Position not set to zero after copy");
        }
Пример #10
0
        public async Task <IActionResult> Get(string setid, string version, string detailLevel, string xpos, string ypos, string zpos, string fmt = null)
        {
            try
            {
                StorageStream modelStream = await Dependency.Storage.GetModelStream(setid, version, detailLevel, xpos, ypos, zpos, fmt);

                return(new StreamResult(modelStream, this.Request));
            }
            catch (NotFoundException)
            {
                return(this.NotFound());
            }
        }
Пример #11
0
        public async Task <IHttpActionResult> Get(string setid, string version, string detail, string xpos, string ypos)
        {
            try
            {
                StorageStream textureStream = await Dependency.Storage.GetTextureStream(setid, version, detail, xpos, ypos);

                return(new StreamResult(textureStream, this.Request));
            }
            catch (NotFoundException)
            {
                return(this.NotFound());
            }
        }
Пример #12
0
        public void TestCopy()
        {
            Assert.IsNotNull(stream);
            StorageStream streamCopy = new StorageStream();

            // set to a non zero position to make sure the copy is reset back to 0
            stream.Position = 2;
            stream.CopyTo(streamCopy);

            Assert.IsNotNull(streamCopy);
            Assert.Equals(stream, streamCopy);
            Assert.IsTrue(streamCopy.Position == 0, "StorageStreamTest: Position not set to zero after copy");
        }
Пример #13
0
        public void TestStreamToTextLarage()
        {
            var stream1 = new StorageStream(new MemoryStorage());
            var builder = new StringBuilder();

            for (var i = 0; i < (ThreadStatic.Buffer.Length / 10) + 1; i++)
            {
                stream1.Write(Encoding.Default.GetBytes("1234567890"), 0, 10);
                builder.Append("1234567890");
            }
            stream1.Seek(0, SeekOrigin.Begin);
            Assert.AreEqual(builder.ToString(), stream1.ToText());
        }
Пример #14
0
        public void TestChineseText()
        {
            var stream1 = new StorageStream(new MemoryStorage());
            var builder = new StringBuilder();

            for (var i = 0; i < (ThreadStatic.Buffer.Length / 10) + 1; i++)
            {
                var data = Encoding.UTF8.GetBytes("12中文34测试的5这是67890");
                stream1.Write(data, 0, data.Length);
                builder.Append("12中文34测试的5这是67890");
            }
            stream1.Seek(0, SeekOrigin.Begin);
            Assert.AreEqual(builder.ToString(), stream1.ToText());
        }
Пример #15
0
        /// <summary>
        /// Images from URL. This is the event handler the model sends when a new image is ready.
        /// It also attempts to save the image to Isolated Storage
        /// </summary>
        /// <param name="stream">The stream.</param>
        public void ImageFromUrl(object o, StorageStream e)
        {
            // Extract the stream from the event args update Image control
            StorageStream s = e;

            BitmapImage image = new BitmapImage();

            image.SetSource(s);
            ImageSource = image;

            // Save it to Isolated Storage
            dataModel.Save(s);
            s.Close();
        }
Пример #16
0
        public PersistentBlock ReadBlock(string primaryKey)
        {
            if (BlockInfoByPrimaryKey.TryGetValue(primaryKey, out var info))
            {
                var block = new PersistentBlock();

                StorageStream.Seek(info.Offset, SeekOrigin.Begin);
                var reader = new BinaryReader(StorageStream);

                block.Read(reader);

                return(block);
            }

            throw new NotSupportedException("primary key not found in backup storage");
        }
Пример #17
0
        public async Task <SynchronizationReport> UploadToAsync(IAsyncFilesSynchronizationCommands destination)
        {
            using (var sourceFileStream = StorageStream.Reading(Storage, FileName))
            {
                var fileSize = sourceFileStream.Length;

                var onlySourceNeed = new List <RdcNeed>
                {
                    new RdcNeed
                    {
                        BlockType   = RdcNeedType.Source,
                        BlockLength = (ulong)fileSize,
                        FileOffset  = 0
                    }
                };

                return(await PushByUsingMultipartRequest(destination, sourceFileStream, onlySourceNeed));
            }
        }
Пример #18
0
        public async Task <SynchronizationReport> UploadToAsync(ISynchronizationServerClient synchronizationServerClient)
        {
            using (var sourceFileStream = StorageStream.Reading(Storage, FileName))
            {
                var fileSize = sourceFileStream.Length;

                var onlySourceNeed = new List <RdcNeed>
                {
                    new RdcNeed
                    {
                        BlockType   = RdcNeedType.Source,
                        BlockLength = (ulong)fileSize,
                        FileOffset  = 0
                    }
                };

                return(await PushByUsingMultipartRequest(synchronizationServerClient, sourceFileStream, onlySourceNeed).ConfigureAwait(false));
            }
        }
Пример #19
0
        private async Task <SynchronizationReport> SynchronizeTo(IAsyncFilesSynchronizationCommands destination,
                                                                 ISignatureRepository localSignatureRepository,
                                                                 ISignatureRepository remoteSignatureRepository,
                                                                 SignatureManifest sourceSignatureManifest,
                                                                 SignatureManifest destinationSignatureManifest)
        {
            var seedSignatureInfo   = SignatureInfo.Parse(destinationSignatureManifest.Signatures.Last().Name);
            var sourceSignatureInfo = SignatureInfo.Parse(sourceSignatureManifest.Signatures.Last().Name);

            using (var localFile = StorageStream.Reading(Storage, FileName))
            {
                IList <RdcNeed> needList;
                using (var needListGenerator = new NeedListGenerator(remoteSignatureRepository, localSignatureRepository))
                {
                    needList = needListGenerator.CreateNeedsList(seedSignatureInfo, sourceSignatureInfo, Cts.Token);
                }

                return(await PushByUsingMultipartRequest(destination, localFile, needList));
            }
        }
Пример #20
0
        public void StorageStream_can_read_overlaping_byte_ranges_from_last_page()
        {
            var buffer = new byte[StorageConstants.MaxPageSize];

            new Random().NextBytes(buffer);

            using (var stream = StorageStream.CreatingNewAndWritting(
                       transactionalStorage, new MockIndexStorage(),
                       new StorageOperationsTask(transactionalStorage, new MockIndexStorage(), new EmptyNotificationsPublisher()),
                       "file", EmptyETagMetadata))
            {
                stream.Write(buffer, 0, StorageConstants.MaxPageSize);
            }

            using (var stream = StorageStream.Reading(transactionalStorage, "file"))
            {
                var readBuffer = new byte[10];

                stream.Seek(StorageConstants.MaxPageSize - 10, SeekOrigin.Begin);
                stream.Read(readBuffer, 0, 10);                 // read last 10 bytes

                var subBuffer = buffer.ToList().Skip(StorageConstants.MaxPageSize - 10).Take(10).ToArray();

                for (int i = 0; i < 10; i++)
                {
                    Assert.Equal(subBuffer[i], readBuffer[i]);
                }

                readBuffer = new byte[5];

                stream.Seek(StorageConstants.MaxPageSize - 5, SeekOrigin.Begin);
                stream.Read(readBuffer, 0, 5);                 // read last 5 bytes - note that they were read last time as well

                subBuffer = buffer.ToList().Skip(StorageConstants.MaxPageSize - 5).Take(5).ToArray();

                for (int i = 0; i < 5; i++)
                {
                    Assert.Equal(subBuffer[i], readBuffer[i]);
                }
            }
        }
Пример #21
0
        public void StorageStream_should_write_to_storage_by_64kB_pages()
        {
            using (var stream = StorageStream.CreatingNewAndWritting(fs, "file", new RavenJObject()))
            {
                var buffer = new byte[StorageConstants.MaxPageSize];

                new Random().NextBytes(buffer);

                stream.Write(buffer, 0, 32768);
                stream.Write(buffer, 32767, 32768);
                stream.Write(buffer, 0, 1);
            }

            FileAndPagesInformation fileAndPages = null;

            transactionalStorage.Batch(accessor => fileAndPages = accessor.GetFile("file", 0, 10));

            Assert.Equal(2, fileAndPages.Pages.Count);
            Assert.Equal(StorageConstants.MaxPageSize, fileAndPages.Pages[0].Size);
            Assert.Equal(1, fileAndPages.Pages[1].Size);
        }
Пример #22
0
        /// <summary>
        /// Handler to process the web request to read an image.  It will also
        /// dispatch a newly created Stream object to the UI thread.
        /// </summary>
        /// <param name="results">The results.</param>
        private void ReadWebRequestHandler(IAsyncResult results)
        {
            try {
                HttpWebRequest  webRequest  = (HttpWebRequest)results.AsyncState;
                HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(results);

                StorageStream streamCopy;

                using (Stream stream = webResponse.GetResponseStream()) {
                    streamCopy = new StorageStream(stream);
                    using (stream) {
                        Deployment.Current.Dispatcher.BeginInvoke(webHandlerMethod, new Object[] { streamCopy });
                    };
                }
                webResponse.Close();
            }
            catch (WebException w) {
                Debug.WriteLine(w);
            }
            finally {
            }
        }
Пример #23
0
        private void ReplaceImage(string file, Guid streamId)
        {
            if (file.Count() > 0)
            {
                if (items == null)
                {
                    items = new List <ImageInfo>();
                }

                using (StorageStream s = storage.OpenStream(streamId))
                {
                    using (FileStream fs = File.OpenRead(file))
                    {
                        byte[] buf = new byte[65536];

                        int l = fs.Read(buf, 0, buf.Length);
                        while (l > 0)
                        {
                            s.Write(buf, 0, l);
                            l = fs.Read(buf, 0, buf.Length);
                        }
                    }

                    ImageInfo ii = items
                                   .Where(i => i.StreamId == streamId)
                                   .First();

                    ii.Name = System.IO.Path.GetFileName(file);
                    ii.Size = (int)s.Length;

                    s.SetLength(s.Position);
                }

                SaveImageList();
                UpdateUI();
                RefreshMap(imageList.SelectedValue as Guid?);
            }
        }
Пример #24
0
        public void StorageStream_can_read_overlaping_byte_ranges_from_last_page()
        {
            var buffer = new byte[StorageConstants.MaxPageSize];

            new Random().NextBytes(buffer);

            using (var stream = StorageStream.CreatingNewAndWritting(fs, "file", new RavenJObject()))
            {
                stream.Write(buffer, 0, StorageConstants.MaxPageSize);
            }

            using (var stream = StorageStream.Reading(fs.Storage, "file"))
            {
                var readBuffer = new byte[10];

                stream.Seek(StorageConstants.MaxPageSize - 10, SeekOrigin.Begin);
                stream.Read(readBuffer, 0, 10); // read last 10 bytes

                var subBuffer = buffer.ToList().Skip(StorageConstants.MaxPageSize - 10).Take(10).ToArray();

                for (int i = 0; i < 10; i++)
                {
                    Assert.Equal(subBuffer[i], readBuffer[i]);
                }

                readBuffer = new byte[5];

                stream.Seek(StorageConstants.MaxPageSize - 5, SeekOrigin.Begin);
                stream.Read(readBuffer, 0, 5); // read last 5 bytes - note that they were read last time as well

                subBuffer = buffer.ToList().Skip(StorageConstants.MaxPageSize - 5).Take(5).ToArray();

                for (int i = 0; i < 5; i++)
                {
                    Assert.Equal(subBuffer[i], readBuffer[i]);
                }
            }
        }
Пример #25
0
        public void StorageStream_should_write_to_storage_by_64kB_pages()
        {
            using (var stream = StorageStream.CreatingNewAndWritting(
                       transactionalStorage, new MockIndexStorage(),
                       new StorageOperationsTask(transactionalStorage, new MockIndexStorage(), new EmptyNotificationsPublisher()),
                       "file", EmptyETagMetadata))
            {
                var buffer = new byte[StorageConstants.MaxPageSize];

                new Random().NextBytes(buffer);

                stream.Write(buffer, 0, 32768);
                stream.Write(buffer, 32767, 32768);
                stream.Write(buffer, 0, 1);
            }

            FileAndPagesInformation fileAndPages = null;

            transactionalStorage.Batch(accessor => fileAndPages = accessor.GetFile("file", 0, 10));

            Assert.Equal(2, fileAndPages.Pages.Count);
            Assert.Equal(StorageConstants.MaxPageSize, fileAndPages.Pages[0].Size);
            Assert.Equal(1, fileAndPages.Pages[1].Size);
        }
Пример #26
0
        public async Task <HttpResponseMessage> MultipartProceed(string fileSystemName)
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var fileName          = Request.Headers.GetValues(SyncingMultipartConstants.FileName).FirstOrDefault();
            var canonicalFilename = FileHeader.Canonize(fileName);

            var tempFileName = RavenFileNameHelper.DownloadingFileName(canonicalFilename);

            var sourceServerInfo = ReadInnerHeaders.Value <ServerInfo>(SyncingMultipartConstants.SourceServerInfo);
            var sourceFileETag   = Guid.Parse(GetHeader(Constants.MetadataEtagField).Trim('\"'));

            var report = new SynchronizationReport(canonicalFilename, sourceFileETag, SynchronizationType.ContentUpdate);

            Log.Debug("Starting to process multipart synchronization request of a file '{0}' with ETag {1} from {2}", fileName, sourceFileETag, sourceServerInfo);

            StorageStream localFile          = null;
            var           isNewFile          = false;
            var           isConflictResolved = false;

            try
            {
                Storage.Batch(accessor =>
                {
                    AssertFileIsNotBeingSynced(canonicalFilename, accessor);
                    FileLockManager.LockByCreatingSyncConfiguration(canonicalFilename, sourceServerInfo, accessor);
                });

                SynchronizationTask.IncomingSynchronizationStarted(canonicalFilename, sourceServerInfo, sourceFileETag, SynchronizationType.ContentUpdate);

                PublishSynchronizationNotification(fileSystemName, canonicalFilename, sourceServerInfo, report.Type, SynchronizationAction.Start);

                Storage.Batch(accessor => StartupProceed(canonicalFilename, accessor));

                RavenJObject sourceMetadata = GetFilteredMetadataFromHeaders(ReadInnerHeaders);

                var localMetadata = GetLocalMetadata(canonicalFilename);
                if (localMetadata != null)
                {
                    AssertConflictDetection(canonicalFilename, localMetadata, sourceMetadata, sourceServerInfo, out isConflictResolved);
                    localFile = StorageStream.Reading(Storage, canonicalFilename);
                }
                else
                {
                    isNewFile = true;
                }

                Historian.UpdateLastModified(sourceMetadata);

                var synchronizingFile = SynchronizingFileStream.CreatingOrOpeningAndWriting(Storage, Search, StorageOperationsTask, tempFileName, sourceMetadata);

                var provider = new MultipartSyncStreamProvider(synchronizingFile, localFile);

                Log.Debug("Starting to process/read multipart content of a file '{0}'", fileName);

                await Request.Content.ReadAsMultipartAsync(provider);

                Log.Debug("Multipart content of a file '{0}' was processed/read", fileName);

                report.BytesCopied     = provider.BytesCopied;
                report.BytesTransfered = provider.BytesTransfered;
                report.NeedListLength  = provider.NumberOfFileParts;

                synchronizingFile.PreventUploadComplete = false;
                synchronizingFile.Flush();
                synchronizingFile.Dispose();
                sourceMetadata["Content-MD5"] = synchronizingFile.FileHash;

                Storage.Batch(accessor => accessor.UpdateFileMetadata(tempFileName, sourceMetadata));

                Storage.Batch(accessor =>
                {
                    StorageOperationsTask.IndicateFileToDelete(canonicalFilename);
                    accessor.RenameFile(tempFileName, canonicalFilename);

                    Search.Delete(tempFileName);
                    Search.Index(canonicalFilename, sourceMetadata);
                });

                if (isNewFile)
                {
                    Log.Debug("Temporary downloading file '{0}' was renamed to '{1}'. Indexes were updated.", tempFileName, fileName);
                }
                else
                {
                    Log.Debug("Old file '{0}' was deleted. Indexes were updated.", fileName);
                }

                if (isConflictResolved)
                {
                    ConflictArtifactManager.Delete(canonicalFilename);
                }
            }
            catch (Exception ex)
            {
                if (ShouldAddExceptionToReport(ex))
                {
                    report.Exception = ex;
                }
            }
            finally
            {
                if (localFile != null)
                {
                    localFile.Dispose();
                }
            }

            if (report.Exception == null)
            {
                Log.Debug(
                    "File '{0}' was synchronized successfully from {1}. {2} bytes were transfered and {3} bytes copied. Need list length was {4}",
                    fileName, sourceServerInfo, report.BytesTransfered, report.BytesCopied, report.NeedListLength);
            }
            else
            {
                Log.WarnException(
                    string.Format("Error has occurred during synchronization of a file '{0}' from {1}", fileName, sourceServerInfo),
                    report.Exception);
            }

            FinishSynchronization(canonicalFilename, report, sourceServerInfo, sourceFileETag);

            PublishFileNotification(fileName, isNewFile ? FileChangeAction.Add : FileChangeAction.Update);
            PublishSynchronizationNotification(fileSystemName, fileName, sourceServerInfo, report.Type, SynchronizationAction.Finish);

            if (isConflictResolved)
            {
                Publisher.Publish(new ConflictNotification
                {
                    FileName = fileName,
                    Status   = ConflictStatus.Resolved
                });
            }

            return(GetMessageWithObject(report));
        }
Пример #27
0
        private void WriteFilesAsAttachments(JsonTextWriter jsonWriter)
        {
            int totalFilesCount = 0;

            fileStorage.Batch(accsesor =>
            {
                totalFilesCount = accsesor.GetFileCount();
            });
            if (totalFilesCount == 0)
            {
                return;
            }
            int currentFilesCount  = 0;
            int previousFilesCount = 0;
            var lastEtag           = Etag.Empty;

            do
            {
                try
                {
                    previousFilesCount = currentFilesCount;
                    fileStorage.Batch(accsesor =>
                    {
                        var fileHeaders = accsesor.GetFilesAfter(lastEtag, batchSize);
                        foreach (var header in fileHeaders)
                        {
                            var file = StorageStream.Reading(fileStorage, header.FullPath);
                            jsonWriter.WriteStartObject();
                            jsonWriter.WritePropertyName("Data");
                            int read = file.Read(filesBuffer, 0, filesBuffer.Length);
                            jsonWriter.WriteRaw("\"");
                            while (read > 0)
                            {
                                var base64 = Convert.ToBase64String(filesBuffer, 0, read);
                                jsonWriter.WriteRaw(base64);
                                jsonWriter.Flush();
                                read = file.Read(filesBuffer, 0, filesBuffer.Length);
                            }
                            jsonWriter.WriteRaw("\"");
                            jsonWriter.SealValue();
                            jsonWriter.WritePropertyName("Metadata");
                            fileMetadata.WriteTo(jsonWriter);
                            jsonWriter.WritePropertyName("Key");
                            jsonWriter.WriteValue(header.FullPath);
                            jsonWriter.WritePropertyName("Etag");
                            jsonWriter.WriteValue(header.Etag.ToString());
                            jsonWriter.WriteEndObject();
                            lastEtag = header.Etag;
                            currentFilesCount++;
                            if (currentFilesCount % batchSize == 0)
                            {
                                ReportProgress("files", currentFilesCount, totalFilesCount);
                            }
                        }
                    });
                }
                catch (Exception e)
                {
                    lastEtag = lastEtag.IncrementBy(1);
                    currentFilesCount++;
                    ReportCorrupted("files", currentFilesCount, e.Message);
                }
                finally
                {
                    if (currentFilesCount > previousFilesCount)
                    {
                        ReportProgress("files", currentFilesCount, totalFilesCount);
                    }
                }
            } while (currentFilesCount < totalFilesCount);
        }
Пример #28
0
        public async Task eventually_will_cleanup_all_deleted_files_even_if_they_use_the_same_pages_and_concurrency_exceptions_are_thrown(string storage)
        {
            var client = NewAsyncClient(requestedStorage: storage);
            var rfs    = GetFileSystem();

            var bytes1 = new byte[1024 * 1024 * 3];
            var bytes2 = new byte[1024 * 1024 * 2];

            var random = new Random();

            random.NextBytes(bytes1);
            random.NextBytes(bytes2);

            await client.UploadAsync("1.bin", new MemoryStream(bytes1));

            await client.UploadAsync("1.bin", new MemoryStream(bytes2)); // will indicate old file to delete

            await client.UploadAsync("1.bin", new MemoryStream(bytes1)); // will indicate old file to delete

            await client.UploadAsync("1.bin", new MemoryStream(bytes2)); // will indicate old file to delete

            await client.UploadAsync("2.bin", new MemoryStream(bytes2));

            await client.UploadAsync("2.bin", new MemoryStream(bytes1)); // will indicate old file to delete

            await client.UploadAsync("2.bin", new MemoryStream(bytes2)); // will indicate old file to delete

            await client.DeleteAsync("1.bin");

            await client.DeleteAsync("2.bin");

            var sw = Stopwatch.StartNew();

            while (sw.Elapsed < TimeSpan.FromMinutes(1))
            {
                try
                {
                    await rfs.Files.CleanupDeletedFilesAsync();

                    break;
                }
                catch (Exception e)
                {
                    if (e.InnerException is ConcurrencyException == false)
                    {
                        throw;
                    }

                    // concurrency exceptions are expected because files indicated to deletion use the same pages
                    // so concurrent modifications can result in concurrency exception
                    // however the first deletion attempt should pass, so finally all deleted files should be cleaned up
                }
            }

            IList <FileHeader> files = null;

            rfs.Storage.Batch(accessor =>
            {
                files = accessor.GetFilesAfter(Etag.Empty, 10).ToList();
            });

            Assert.Equal(2, files.Count);
            Assert.True(files.All(x => x.IsTombstone));

            // but after upload there should be two files which aren't tombstones

            await client.UploadAsync("1.bin", new MemoryStream(bytes1));

            await client.UploadAsync("2.bin", new MemoryStream(bytes1));

            rfs.Storage.Batch(accessor =>
            {
                files = accessor.GetFilesAfter(Etag.Empty, 10).ToList();
            });

            Assert.Equal(2, files.Count);
            Assert.False(files.All(x => x.IsTombstone));

            foreach (var file in files)
            {
                var content = new MemoryStream(new byte[file.TotalSize.Value]);

                await StorageStream.Reading(rfs.Storage, files[0].FullPath).CopyToAsync(content);

                Assert.Equal(bytes1, content.ToArray());
            }
        }
Пример #29
0
 public StreamResult(StorageStream storageStream, HttpRequestMessage request)
 {
     this._storageStream = storageStream;
     this._request       = request;
 }
Пример #30
0
        /// <summary>
        /// Images from URL. This is the event handler the model sends when a new image is ready.
        /// It also attempts to save the image to Isolated Storage
        /// </summary>
        /// <param name="stream">The stream.</param>
        public void ImageFromUrl(object o, StorageStream e)
        {
            // Extract the stream from the event args update Image control
            StorageStream s = e;

            BitmapImage image = new BitmapImage();
            image.SetSource(s);
            ImageSource = image;

            // Save it to Isolated Storage
            dataModel.Save(s);
            s.Close();
        }
Пример #31
0
        /// <summary>
        /// Handler to process the web request to read an image.  It will also
        /// dispatch a newly created Stream object to the UI thread.
        /// </summary>
        /// <param name="results">The results.</param>
        private void ReadWebRequestHandler(IAsyncResult results)
        {
            try {
                HttpWebRequest webRequest = (HttpWebRequest)results.AsyncState;
                HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(results);

                StorageStream streamCopy;

                using (Stream stream = webResponse.GetResponseStream()) {
                    streamCopy = new StorageStream(stream);
                    using (stream) {
                        Deployment.Current.Dispatcher.BeginInvoke(webHandlerMethod, new Object[] { streamCopy });
                    };
                }
                webResponse.Close();
            }
            catch (WebException w) {
                Debug.WriteLine(w);
            }
            finally {
            }
        }
Пример #32
0
        private async Task ExecuteContentUpdate(RavenJObject localMetadata, SynchronizationReport report)
        {
            var tempFileName = RavenFileNameHelper.DownloadingFileName(fileName);

            using (var localFile = localMetadata != null ? StorageStream.Reading(fs.Storage, fileName) : null)
            {
                fs.PutTriggers.Apply(trigger => trigger.OnPut(tempFileName, sourceMetadata));

                fs.Historian.UpdateLastModified(sourceMetadata);

                var synchronizingFile = SynchronizingFileStream.CreatingOrOpeningAndWriting(fs, tempFileName, sourceMetadata);

                fs.PutTriggers.Apply(trigger => trigger.AfterPut(tempFileName, null, sourceMetadata));

                var provider = new MultipartSyncStreamProvider(synchronizingFile, localFile);

                if (Log.IsDebugEnabled)
                {
                    Log.Debug("Starting to process/read multipart content of a file '{0}'", fileName);
                }

                await MultipartContent.ReadAsMultipartAsync(provider).ConfigureAwait(false);

                if (Log.IsDebugEnabled)
                {
                    Log.Debug("Multipart content of a file '{0}' was processed/read", fileName);
                }

                report.BytesCopied     = provider.BytesCopied;
                report.BytesTransfered = provider.BytesTransfered;
                report.NeedListLength  = provider.NumberOfFileParts;

                synchronizingFile.PreventUploadComplete = false;
                synchronizingFile.Flush();
                synchronizingFile.Dispose();
                sourceMetadata["Content-MD5"] = synchronizingFile.FileHash;

                FileUpdateResult updateResult = null;
                fs.Storage.Batch(accessor => updateResult = accessor.UpdateFileMetadata(tempFileName, sourceMetadata, null));

                fs.Storage.Batch(accessor =>
                {
                    using (fs.DisableAllTriggersForCurrentThread())
                    {
                        fs.Files.IndicateFileToDelete(fileName, null);
                    }

                    accessor.RenameFile(tempFileName, fileName);

                    fs.Search.Delete(tempFileName);
                    fs.Search.Index(fileName, sourceMetadata, updateResult.Etag);
                });

                if (Log.IsDebugEnabled)
                {
                    var message = localFile == null
                        ? string.Format("Temporary downloading file '{0}' was renamed to '{1}'. Indexes were updated.", tempFileName, fileName)
                        : string.Format("Old file '{0}' was deleted. Indexes were updated.", fileName);

                    Log.Debug(message);
                }

                fs.Publisher.Publish(new FileChangeNotification {
                    File = fileName, Action = localFile == null ? FileChangeAction.Add : FileChangeAction.Update
                });
            }
        }
Пример #33
0
 /// <summary>
 /// Occurs when [image changed].  Subscribe to this event to 
 /// receive the Image stream when is arrives.
 /// </summary>
 protected void OnImageChanged(StorageStream stream)
 {
     nc.Send<StorageStream>(this, stream);
 }
Пример #34
0
 /// <summary>
 /// Saves the specified stream to Isolated Storage
 /// </summary>
 /// <param name="stream">The stream.</param>
 public virtual void Save(StorageStream stream)
 {
     phoneStorage.Save(stream);
 }
Пример #35
0
 public void Setup()
 {
     stream = new StorageStream();
 }
Пример #36
0
        private IEnumerable <SignatureInfo> PrepareSignatures(string filename)
        {
            var input = StorageStream.Reading(_transactionalStorage, filename);

            return(_sigGenerator.GenerateSignatures(input, filename, _signatureRepository));
        }