private static void ThrowsNotSupportedException(LengthStream lengthStream, Action <LengthStream> exceptionAction) { Assert.Throws <NotSupportedException>(delegate { // ReSharper disable once UnusedVariable exceptionAction(lengthStream); }); }
private static void ThrowsNotSupportedException <T>(LengthStream lengthStream, Func <LengthStream, T> exceptionFunc) { Assert.Throws <NotSupportedException>(delegate { // ReSharper disable once UnusedVariable exceptionFunc(lengthStream); }); }
public void StreamTests_Throws_NotSupportedException() { using (var memoryStream = new MemoryStream()) using (var lengthStream = new LengthStream(memoryStream, 3)) { var buffer = new byte[10]; ThrowsNotSupportedException(lengthStream, stream => stream.Position = 5); ThrowsNotSupportedException(lengthStream, stream => stream.Seek(0, SeekOrigin.Begin)); ThrowsNotSupportedException(lengthStream, stream => stream.Write(buffer, 0, buffer.Length)); ThrowsNotSupportedException(lengthStream, stream => stream.SetLength(7)); ThrowsNotSupportedException(lengthStream, stream => stream.Flush()); } }
int ValidateAndGetLengthCore(IGeometry value) { if (_lengthStream == null) { _lengthStream = new LengthStream(); } else { _lengthStream.SetLength(0); } _writer.Write(value, _lengthStream); return((int)_lengthStream.Length); }
public void Length_AsExpected() { long trueLength, modifiedLength; using (var memoryStream = new MemoryStream()) { using (var writer = new StreamWriter(memoryStream, Encoding.ASCII, 1024, true)) { writer.Write("The quick brown fox jumps over the lazy dog"); } trueLength = memoryStream.Length; using (var lengthStream = new LengthStream(memoryStream, 3)) { modifiedLength = lengthStream.Length; } } Assert.Equal(43, trueLength); Assert.Equal(3, modifiedLength); }
internal static bool TryDecryptUpload(this IS3Client s3Client, string bucketName, string key, string filePath, AesCryptoServiceProvider aes, FileMetadata metadata) { try { using (var fileStream = FileUtilities.GetReadStream(filePath)) using (var cryptoStream = new CryptoStream(fileStream, aes.CreateDecryptor(), CryptoStreamMode.Read)) using (var lengthStream = new LengthStream(cryptoStream, metadata.Length)) { string md5String = Convert.ToBase64String(metadata.MD5); var request = new PutObjectRequest { BucketName = bucketName, Key = key, InputStream = lengthStream, MD5Digest = md5String }; s3Client.PutObjectAsync(request).Wait(); } return(true); } catch (Exception exception) { var processedException = ExceptionUtilities.ProcessAmazonS3Exception(exception, null); if (processedException is UserErrorException) { throw processedException; } Logger.LogLine($"Exception: {exception.Message}."); return(false); } }
public void StreamTests_AsExpected() { using (var memoryStream = new MemoryStream()) { using (var writer = new StreamWriter(memoryStream, Encoding.ASCII, 1024, true)) { writer.Write("The quick brown fox jumps over the lazy dog"); } long expectedPosition = memoryStream.Position; memoryStream.Position = 0; using (var lengthStream = new LengthStream(memoryStream, 3)) using (var reader = new StreamReader(lengthStream)) { reader.ReadToEnd(); Assert.True(lengthStream.CanRead); Assert.True(lengthStream.CanWrite); Assert.True(lengthStream.CanSeek); Assert.Equal(3, lengthStream.Length); Assert.True(lengthStream.Position >= expectedPosition); } } }
public async Task <IActionResult> Zip(string ids, bool?compressed, CancellationToken token) { //var r = BadRequestIfPasswordInvalid(); //if (r != null) return r; Response.ContentType = "application/zip"; Response.Headers.Add("Content-Disposition", $"attachment;filename=photobin_{DateTime.UtcNow.ToString("yyyyMMdd-hhmmss")}.zip"); IEnumerable <Guid> guids = ids.Split('\r', '\n', ',', ';').Where(s => s != "").Select(s => Guid.Parse(s)); var fileMetadata = await _context.FileMetadata.Where(f => guids.Contains(f.FileMetadataId)).ToListAsync(); var compressionLevel = compressed == true ? CompressionLevel.Optimal : CompressionLevel.NoCompression; if (compressionLevel == CompressionLevel.NoCompression) { // The size of a .zip file with no compression can be determined from just the names and sizes of the files. using (var s = new LengthStream()) { using (var archive = new ZipArchive(s, ZipArchiveMode.Create, true)) { foreach (var file in fileMetadata) { token.ThrowIfCancellationRequested(); byte[] data = new byte[file.Size]; var entry = archive.CreateEntry(file.NewFilename, compressionLevel); entry.LastWriteTime = file.UploadedAt; using (var zipStream = entry.Open()) { await zipStream.WriteAsync(data, 0, data.Length); } } } Response.Headers.Add("Content-Length", s.Position.ToString()); } } using (var s = Response.Body) { using (var archive = new ZipArchive(s, ZipArchiveMode.Create, true)) { foreach (var file in fileMetadata) { token.ThrowIfCancellationRequested(); var container = await GetCloudBlobContainerAsync(); CloudBlockBlob full = container.GetBlockBlobReference($"full-{file.FileMetadataId}"); if (!await full.ExistsAsync()) { throw new Exception($"{file.FileMetadataId} not found"); } var entry = archive.CreateEntry(file.NewFilename, compressionLevel); entry.LastWriteTime = file.UploadedAt; byte[] data = new byte[1024 * 1024 * 8]; using (var zipStream = entry.Open()) { for (int offset = 0; offset < file.Size; offset += data.Length) { int length = Math.Min(data.Length, file.Size - offset); await full.DownloadRangeToByteArrayAsync(data, 0, offset, length); await zipStream.WriteAsync(data, 0, length); } } } } } return(new EmptyResult()); }