protected void BlobStreamStore(Guid id, Stream stream) { var blob = Provider.BlobRetrieve(id); if (blob.StorageKey != null) { List<BlobPart> existingParts; using (var partStream = StorageAdapter.Read(blob.StorageKey)) { existingParts = JsonSerializer.Deserialize<List<BlobPart>>( new JsonTextReader(new StreamReader(partStream))); } if (existingParts == null) throw new ApplicationException("Could not deserialize existing parts"); bool success = existingParts.Aggregate(true, (current, part) => current & StorageAdapter.Delete(part.ExternalStorageKey)); if (!success) throw new ApplicationException("Could not clean up existing parts"); } using (var streamPartsStream = StreamUtility.GetTempFileStream()) using (var streamWriter = new StreamWriter(streamPartsStream)) { var streamParts = StreamUtility.SplitStream(stream, Config.StorageBlobChunkSize, (streamChunk, checkSum, partId) => { BlobPart part = new BlobPart(); part.Part = partId; part.CheckSum = checkSum; part.ExternalStorageKey = StorageAdapter.Create(streamChunk); return part; }); JsonSerializer.Serialize(streamWriter, streamParts); streamWriter.Flush(); streamPartsStream.Position = 0; blob.StorageKey = StorageAdapter.Create(streamPartsStream); Provider.BlobUpdate(blob); } }
public bool PutData(IUser user, Guid id, HttpRequestMessage request) { LogVerbose("BlobDataPut entering."); LogDebug(String.Format("id = '{0}'", id)); if (!IsAdministrator(user) && !IsBlobOwned(user, id)) throw new UnauthorizedAccessException(); var blob = Provider.BlobRetrieve(id); if (blob.StorageKey != null) { LogDebug(string.Format("Existing StorageKey: {0}", blob.StorageKey)); List<BlobPart> existingParts; using (var partStream = StorageAdapter.Read(blob.StorageKey)) { existingParts = JsonSerializer.Deserialize<List<BlobPart>>(new JsonTextReader(new StreamReader(partStream))); } if (existingParts == null) throw new ApplicationException("Could not deserialize existing parts"); bool success = existingParts.Aggregate(true, (current, part) => current & StorageAdapter.Delete(part.ExternalStorageKey)); if (!success) throw new ApplicationException("Could not clean up existing parts"); } using (var streamPartsStream = StreamUtility.GetTempFileStream()) using (var streamWriter = new StreamWriter(streamPartsStream)) { LogDebug("Starting stream splitter"); List<BlobPart> streamParts; try { streamParts = StreamUtility.SplitStream(HttpContext.Request.Body, Config.StorageBlobChunkSize, (streamChunk, checkSum, partId) => { LogVerbose(string.Format("Storing StreamChunk for BlobPartId: {0}", partId)); BlobPart part = new BlobPart(); part.Part = partId; part.CheckSum = checkSum; part.ExternalStorageKey = StorageAdapter.Create(streamChunk); LogDebug(string.Format("BlobPart: {0}", part.ToString())); return part; }, LogDebug); } catch (Exception ex) { LogError("Could not split stream.", ex); throw; } JsonSerializer.Serialize(streamWriter, streamParts); streamWriter.Flush(); streamPartsStream.Position = 0; blob.StorageKey = StorageAdapter.Create(streamPartsStream); LogVerbose("PutData leaving."); return Provider.BlobUpdate(blob); } }