CopyToAsync() публичный Метод

public CopyToAsync ( Stream destination, int bufferSize, CancellationToken cancellationToken ) : Task
destination Stream
bufferSize int
cancellationToken CancellationToken
Результат Task
 public async Task Perform()
 {
     using (var sourceStream = new FileStream(_oldPath, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, FileOptions.Asynchronous | FileOptions.DeleteOnClose))
     {
         using (var destinationStream = new FileStream(_newPath, FileMode.Create, FileAccess.Write, FileShare.None, DefaultBufferSize, FileOptions.Asynchronous))
         {
             await sourceStream.CopyToAsync(destinationStream);
         }
     }
 }
		/// <summary>
		/// Read all bytes from a specified file asynchronously.
		/// </summary>
		/// <param name="filePath">File path</param>
		/// <param name="bufferSize">Buffer size</param>
		/// <param name="cancellationToken">CancellationToken</param>
		/// <returns>Byte array of file</returns>
		public static async Task<byte[]> ReadAllBytesAsync(string filePath, int bufferSize, CancellationToken cancellationToken)
		{
			using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
			using (var ms = new MemoryStream())
			{
				await fs.CopyToAsync(ms, bufferSize, cancellationToken).ConfigureAwait(false);
				return ms.ToArray();
			}
		}
Пример #3
0
		public async Task TranscodeInternal(CancellationToken ct, IWaveStreamProvider stream, Stream targetStream)
		{
			var tempFilename = Path.Combine(Path.GetTempPath(), GetTranscodedFileName(Guid.NewGuid().ToString() + ".extension"));			
			MediaFoundationEncoder.EncodeToMp3(stream, tempFilename, 320000);			
			using (var tempStream = new FileStream(tempFilename, FileMode.Open, FileAccess.Read))
			{
				await tempStream.CopyToAsync(targetStream, 81920, ct);
			}
			File.Delete(tempFilename);
		}
            public async Task CopyTo(string destination) {
                if (!File.Exists(_filename)) {
                    throw new FileNotFoundException(ToolsStrings.DirectoryInstallator_FileNotFound, _filename);
                }

                using (var input = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
                using (var output = new FileStream(destination, FileMode.Create)) {
                    await input.CopyToAsync(output);
                }
            }
Пример #5
0
 public static async Task<byte[]> LoadToBuffer(string FileName) {
     byte[] buffer = null;
     var f = Application.Environment.ApplicationBasePath + "/" + FileName;
     using (var fs = new FileStream(f, FileMode.Open, FileAccess.Read)) {
         using (var ms = new MemoryStream()) {
             await fs.CopyToAsync(ms);
             buffer = ms.ToArray();
         }
     }
     return buffer;
 }
Пример #6
0
 private async Task BackupROM()
 {
     // Copy the original file here with a .bak extension
     using (FileStream original = new FileStream(romPath, FileMode.Open, FileAccess.Read, FileShare.None, 4096, true))
     {
         using (FileStream backup = new FileStream(romPath + ".bak", FileMode.Create, FileAccess.Write, FileShare.None, 4096, true))
         {
             await original.CopyToAsync(backup);
         }
     }
 }
Пример #7
0
        /// <summary>
        /// Executes the result against the provided service context asynchronously.
        /// </summary>
        /// <param name="context">The service context.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A task executing the result.</returns>
        public virtual async Task ExecuteAsync(IServiceContext context, CancellationToken cancellationToken)
        {
            context.Response.Output.Buffer = false;
            context.Response.Output.Clear();

            string etag = context.Response.GenerateEtag(FilePath);

            if (etag == context.Request.Headers.TryGet(IfNoneMatchHeaderName))
            {
                context.Response.SetStatus(HttpStatusCode.NotModified);
                return;
            }

            try
            {
                using (var fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    context.Response.SetHeader(context.Response.HeaderNames.ContentLength, fileStream.Length.ToString(CultureInfo.InvariantCulture));
                    context.Response.SetHeader(context.Response.HeaderNames.ContentType, ContentType ?? DefaultHtmlContentType);
                    context.Response.SetHeader(context.Response.HeaderNames.ETag, etag);
                    context.Response.SetCharsetEncoding(context.Request.Headers.AcceptCharsetEncoding);

                    await fileStream.CopyToAsync(context.Response.Output.Stream, Convert.ToInt32(fileStream.Length), cancellationToken);
                }
            }
            catch (ArgumentException)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError, Global.InvalidFilePathOrUrl);
            }
            catch (FileNotFoundException)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError, Global.InvalidFilePathOrUrl);
            }
            catch (DirectoryNotFoundException)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound, Global.InvalidFilePathOrUrl);
            }
            catch (PathTooLongException)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound, Global.InvalidFilePathOrUrl);
            }
            catch (IOException)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError, Global.InaccessibleFile);
            }
            catch (SecurityException)
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden, Global.InaccessibleFile);
            }
            catch (UnauthorizedAccessException)
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden, Global.InaccessibleFile);
            }
        }
 public Task CopyToAsync(MemoryStream memStream)
 {
     if (YetaWFManager.IsSync())
     {
         Stream.CopyTo(memStream);
         return(Task.CompletedTask);
     }
     else
     {
         return(Stream.CopyToAsync(memStream));
     }
 }
Пример #9
0
 public void Configure(IApplicationBuilder app)
 {
     app.Run(async (ctx) =>
     {
         var filePath = Path.Combine(_env.ApplicationBasePath, "Testfile");
         using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 1024 * 64,
             FileOptions.Asynchronous | FileOptions.SequentialScan))
         {
             await fileStream.CopyToAsync(ctx.Response.Body);
         }
     });
 }
 public async Task UploadAsync(string url, IDictionary<string, string> headers, string method, string file)
 {
     byte[] data = null;
     using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, 1024, true))
     {
         using (var memStream = new MemoryStream())
         {
             await fileStream.CopyToAsync(memStream);
             data = memStream.ToArray();
         }
     }
     await UploadAsync(url, headers, method, data);
 }
        async Task<Stream> IMessageDataRepository.Get(Uri address, CancellationToken cancellationToken)
        {
            string filePath = ParseFilePath(address);

            string fullPath = Path.Combine(_dataDirectory.FullName, filePath);
            if (!File.Exists(fullPath))
                throw new FileNotFoundException("The file was not found", fullPath);

            using (var stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, FileOptions.Asynchronous))
            {
                var memoryStream = new MemoryStream();
                await stream.CopyToAsync(memoryStream, DefaultBufferSize, cancellationToken);
                
                memoryStream.Position = 0;
                return memoryStream;
            }
        }
Пример #12
0
 public async Task<MemoryStream> RetrieveData(Guid sourceId, string currentFileName)
 {
     if (!File.Exists(currentFileName)) return (MemoryStream) Stream.Null;
     using (var sourceStream =
         new FileStream(
             currentFileName,
             FileMode.Open,
             FileAccess.Read,
             FileShare.Read,
             4096,
             true))
     {
         var destination = new MemoryStream();
         await sourceStream.CopyToAsync(destination);
         return destination;
     }
 }
        public async Task<HttpResponseMessage> GetUncompressedImage()
        {
            using (var ms = new MemoryStream())
            {
                var baseDir = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent;
                var filePath = baseDir.FullName + "/Content/Images/app-icon-1024.png";

                using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    fs.Position = 0;
                    await fs.CopyToAsync(ms);
                }

                var result = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(ms.ToArray())
                };
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");

                return result;
            }
        }
        public async Task Invoke(IDictionary<string, object> env)
        {
            var req = new OwinRequest(env);
            string file;
            if (!_router.TryGet(req.Path, out file))
            {
                await _next(env);
            }

            var resp = new OwinResponse(req)
            {
                ContentType = "text/html"
            };

            if (!File.Exists(file))
            {
                resp.StatusCode = 404;
                await resp.WriteAsync("File not found");
            }
            using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                await fs.CopyToAsync(resp.Body);
            }
        }
        public async Task Invoke(HttpContext httpContext)
        {
            string currentUrl = httpContext.Request.GetDisplayUrl();

            string filePath = _routingService.GetPathForUrl(currentUrl);

            if (filePath != null)
            {
                string fileExtension = GetFileExtension(filePath);

                httpContext.Response.ContentType = _mimeTypeResolver[fileExtension];

                httpContext.Response.StatusCode = 200;

                using (var fileStream = new FileStream(filePath, FileMode.Open))
                {
                    await fileStream.CopyToAsync(httpContext.Response.Body);
                }
            }
            else
            {
                httpContext.Response.StatusCode = 404;
            }
        }
 public override async Task Invoke(IOwinContext context)
 {
     RequestContentType requestContentType = GetContentType(context.Request.Path.Value);
     if(requestContentType != null)
     {
         context.Response.ContentType = requestContentType.ContentType;
         string filePath = MapPath(context.Request.Path.Value);
         if (File.Exists(filePath))
         {
             using (var contentStream = new FileStream(filePath, FileMode.Open))
             {
                 await contentStream.CopyToAsync(context.Response.Body);
             }
         }
         else
         {
             await Next.Invoke(context);
         }
     }
     else
     {
         await Next.Invoke(context);
     }
 }
Пример #17
0
        /// <summary>
        /// Crops whitespace from an image, caches the result, and returns the cached path
        /// </summary>
        /// <param name="originalImagePath">The original image path.</param>
        /// <param name="dateModified">The date modified.</param>
        /// <returns>System.String.</returns>
        private async Task<string> GetCroppedImage(string originalImagePath, DateTime dateModified)
        {
            var name = originalImagePath;
            name += "datemodified=" + dateModified.Ticks;

            var croppedImagePath = CroppedImageCache.GetResourcePath(name, Path.GetExtension(originalImagePath));

            var semaphore = GetLock(croppedImagePath);

            await semaphore.WaitAsync().ConfigureAwait(false);

            // Check again in case of contention
            if (File.Exists(croppedImagePath))
            {
                semaphore.Release();
                return croppedImagePath;
            }

            try
            {
                using (var fileStream = new FileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
                {
                    // Copy to memory stream to avoid Image locking file
                    using (var memoryStream = new MemoryStream())
                    {
                        await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);

                        using (var originalImage = (Bitmap)Image.FromStream(memoryStream, true, false))
                        {
                            var outputFormat = originalImage.RawFormat;

                            using (var croppedImage = originalImage.CropWhitespace())
                            {
                                var parentPath = Path.GetDirectoryName(croppedImagePath);

                                if (!Directory.Exists(parentPath))
                                {
                                    Directory.CreateDirectory(parentPath);
                                }

                                using (var outputStream = new FileStream(croppedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read))
                                {
                                    croppedImage.Save(outputFormat, outputStream, 100);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // We have to have a catch-all here because some of the .net image methods throw a plain old Exception
                _logger.ErrorException("Error cropping image {0}", ex, originalImagePath);

                return originalImagePath;
            }
            finally
            {
                semaphore.Release();
            }

            return croppedImagePath;
        }
Пример #18
0
        /// <summary>
        /// Runs an image through the image enhancers, caches the result, and returns the cached path
        /// </summary>
        /// <param name="originalImagePath">The original image path.</param>
        /// <param name="dateModified">The date modified of the original image file.</param>
        /// <param name="item">The item.</param>
        /// <param name="imageType">Type of the image.</param>
        /// <param name="imageIndex">Index of the image.</param>
        /// <param name="supportedEnhancers">The supported enhancers.</param>
        /// <returns>System.String.</returns>
        /// <exception cref="System.ArgumentNullException">originalImagePath</exception>
        public async Task<string> GetEnhancedImage(string originalImagePath, DateTime dateModified, BaseItem item, ImageType imageType, int imageIndex, IEnumerable<IImageEnhancer> supportedEnhancers)
        {
            if (string.IsNullOrEmpty(originalImagePath))
            {
                throw new ArgumentNullException("originalImagePath");
            }

            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            var cacheGuid = GetImageCacheTag(originalImagePath, dateModified, supportedEnhancers, item, imageType);

            // All enhanced images are saved as png to allow transparency
            var enhancedImagePath = EnhancedImageCache.GetResourcePath(cacheGuid + ".png");

            var semaphore = GetLock(enhancedImagePath);

            await semaphore.WaitAsync().ConfigureAwait(false);

            // Check again in case of contention
            if (File.Exists(enhancedImagePath))
            {
                semaphore.Release();
                return enhancedImagePath;
            }

            try
            {
                using (var fileStream = new FileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
                {
                    // Copy to memory stream to avoid Image locking file
                    using (var memoryStream = new MemoryStream())
                    {
                        await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);

                        using (var originalImage = Image.FromStream(memoryStream, true, false))
                        {
                            //Pass the image through registered enhancers
                            using (var newImage = await ExecuteImageEnhancers(supportedEnhancers, originalImage, item, imageType, imageIndex).ConfigureAwait(false))
                            {
                                var parentDirectory = Path.GetDirectoryName(enhancedImagePath);

                                if (!Directory.Exists(parentDirectory))
                                {
                                    Directory.CreateDirectory(parentDirectory);
                                }

                                //And then save it in the cache
                                using (var outputStream = new FileStream(enhancedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read))
                                {
                                    newImage.Save(ImageFormat.Png, outputStream, 100);
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                semaphore.Release();
            }

            return enhancedImagePath;
        }
Пример #19
0
        internal async Task<HttpSourceResult> GetAsync(string uri, string cacheKey, TimeSpan cacheAgeLimit,
            Action<Stream> ensureValidContents = null, bool throwNotFound = true)
        {
            try
            {
                await _throttle.WaitAsync();

                Stopwatch sw = new Stopwatch();
                sw.Start();

                var result = await TryCache(uri, cacheKey, cacheAgeLimit);
                if (result.Stream != null)
                {
                    _reports.Quiet.WriteLine(string.Format("  {0} {1}", "CACHE".Green(), uri));
                    return result;
                }

                _reports.Quiet.WriteLine(string.Format("  {0} {1}", "GET".Yellow(), uri));

                var request = new HttpRequestMessage(HttpMethod.Get, uri);
                if (_userName != null)
                {
                    var token = Convert.ToBase64String(Encoding.ASCII.GetBytes(_userName + ":" + _password));
                    request.Headers.Authorization = new AuthenticationHeaderValue("Basic", token);
                };

                // Write the response to a file and use the file stream to avoid memory explosion
                using (var responseStream = new FileStream(
                    Path.GetTempFileName(),
                    FileMode.Create,
                    FileAccess.ReadWrite,
                    FileShare.None,
                    bufferSize: 8192,
                    options: FileOptions.Asynchronous | FileOptions.DeleteOnClose))
                {
                    HttpStatusCode statusCode;

                    using (var response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
                    {
                        if (!throwNotFound && response.StatusCode == HttpStatusCode.NotFound)
                        {
                            _reports.Quiet.WriteLine(
                                $"  {response.StatusCode.ToString().Green()} {uri} {sw.ElapsedMilliseconds.ToString().Bold()}ms");
                            return new HttpSourceResult();
                        }

                        response.EnsureSuccessStatusCode();
                        statusCode = response.StatusCode;
                        await response.Content.CopyToAsync(responseStream);
                    }

                    responseStream.Seek(0, SeekOrigin.Begin);
                    ensureValidContents?.Invoke(responseStream);

                    var newFile = result.CacheFileName + "-new";

                    // Zero value of TTL means we always download the latest package
                    // So we write to a temp file instead of cache
                    if (cacheAgeLimit.Equals(TimeSpan.Zero))
                    {
                        result.CacheFileName = Path.GetTempFileName();
                        newFile = Path.GetTempFileName();
                    }

                    // The update of a cached file is divided into two steps:
                    // 1) Delete the old file. 2) Create a new file with the same name.
                    // To prevent race condition among multiple processes, here we use a lock to make the update atomic.
                    await ConcurrencyUtilities.ExecuteWithFileLocked(result.CacheFileName, action: async _ =>
                    {
                        using (var stream = CreateAsyncFileStream(
                            newFile,
                            FileMode.Create,
                            FileAccess.ReadWrite,
                            FileShare.ReadWrite | FileShare.Delete))
                        {
                            responseStream.Seek(0, SeekOrigin.Begin);
                            await responseStream.CopyToAsync(stream);
                            await stream.FlushAsync();
                        }

                        if (File.Exists(result.CacheFileName))
                        {
                        // Process B can perform deletion on an opened file if the file is opened by process A
                        // with FileShare.Delete flag. However, the file won't be actually deleted until A close it.
                        // This special feature can cause race condition, so we never delete an opened file.
                        if (!IsFileAlreadyOpen(result.CacheFileName))
                            {
                                File.Delete(result.CacheFileName);
                            }
                        }

                        // If the destination file doesn't exist, we can safely perform moving operation.
                        // Otherwise, moving operation will fail.
                        if (!File.Exists(result.CacheFileName))
                        {
                            File.Move(
                                newFile,
                                result.CacheFileName);
                        }

                        // Even the file deletion operation above succeeds but the file is not actually deleted,
                        // we can still safely read it because it means that some other process just updated it
                        // and we don't need to update it with the same content again.
                        result.Stream = CreateAsyncFileStream(
                            result.CacheFileName,
                            FileMode.Open,
                            FileAccess.Read,
                            FileShare.Read | FileShare.Delete);

                        return 0;
                    });

                    _reports.Quiet.WriteLine(string.Format("  {1} {0} {2}ms", uri, statusCode.ToString().Green(), sw.ElapsedMilliseconds.ToString().Bold()));
                }

                return result;
            }
            finally
            {
                _throttle.Release();
            }
        }
Пример #20
0
        /// <summary>
        /// Processes an image by resizing to target dimensions
        /// </summary>
        /// <param name="entity">The entity that owns the image</param>
        /// <param name="imageType">The image type</param>
        /// <param name="imageIndex">The image index (currently only used with backdrops)</param>
        /// <param name="originalImagePath">The original image path.</param>
        /// <param name="cropWhitespace">if set to <c>true</c> [crop whitespace].</param>
        /// <param name="dateModified">The last date modified of the original image file</param>
        /// <param name="toStream">The stream to save the new image to</param>
        /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
        /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
        /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
        /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
        /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
        /// <param name="enhancers">The enhancers.</param>
        /// <returns>Task.</returns>
        /// <exception cref="System.ArgumentNullException">entity</exception>
        public async Task ProcessImage(BaseItem entity, ImageType imageType, int imageIndex, string originalImagePath, bool cropWhitespace, DateTime dateModified, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight, int? quality, List<IImageEnhancer> enhancers)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            if (toStream == null)
            {
                throw new ArgumentNullException("toStream");
            }

            if (cropWhitespace)
            {
                originalImagePath = await GetCroppedImage(originalImagePath, dateModified).ConfigureAwait(false);
            }

            // No enhancement - don't cache
            if (enhancers.Count > 0)
            {
                try
                {
                    // Enhance if we have enhancers
                    var ehnancedImagePath = await GetEnhancedImage(originalImagePath, dateModified, entity, imageType, imageIndex, enhancers).ConfigureAwait(false);

                    // If the path changed update dateModified
                    if (!ehnancedImagePath.Equals(originalImagePath, StringComparison.OrdinalIgnoreCase))
                    {
                        dateModified = File.GetLastWriteTimeUtc(ehnancedImagePath);
                        originalImagePath = ehnancedImagePath;
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error("Error enhancing image", ex);
                }
            }

            var originalImageSize = await GetImageSize(originalImagePath, dateModified).ConfigureAwait(false);

            // Determine the output size based on incoming parameters
            var newSize = DrawingUtils.Resize(originalImageSize, width, height, maxWidth, maxHeight);

            if (!quality.HasValue)
            {
                quality = 90;
            }

            var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality.Value, dateModified);

            try
            {
                using (var fileStream = new FileStream(cacheFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
                {
                    await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
                    return;
                }
            }
            catch (IOException)
            {
                // Cache file doesn't exist or is currently being written ro
            }

            var semaphore = GetLock(cacheFilePath);

            await semaphore.WaitAsync().ConfigureAwait(false);

            // Check again in case of lock contention
            if (File.Exists(cacheFilePath))
            {
                try
                {
                    using (var fileStream = new FileStream(cacheFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
                    {
                        await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
                        return;
                    }
                }
                finally
                {
                    semaphore.Release();
                }
            }

            try
            {
                using (var fileStream = new FileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
                {
                    // Copy to memory stream to avoid Image locking file
                    using (var memoryStream = new MemoryStream())
                    {
                        await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);

                        using (var originalImage = Image.FromStream(memoryStream, true, false))
                        {
                            var newWidth = Convert.ToInt32(newSize.Width);
                            var newHeight = Convert.ToInt32(newSize.Height);

                            // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
                            using (var thumbnail = !ImageExtensions.IsPixelFormatSupportedByGraphicsObject(originalImage.PixelFormat) ? new Bitmap(originalImage, newWidth, newHeight) : new Bitmap(newWidth, newHeight, originalImage.PixelFormat))
                            {
                                // Preserve the original resolution
                                thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);

                                using (var thumbnailGraph = Graphics.FromImage(thumbnail))
                                {
                                    thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
                                    thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
                                    thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                    thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
                                    thumbnailGraph.CompositingMode = CompositingMode.SourceOver;

                                    thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight);

                                    var outputFormat = originalImage.RawFormat;

                                    using (var outputMemoryStream = new MemoryStream())
                                    {
                                        // Save to the memory stream
                                        thumbnail.Save(outputFormat, outputMemoryStream, quality.Value);

                                        var bytes = outputMemoryStream.ToArray();

                                        var outputTask = toStream.WriteAsync(bytes, 0, bytes.Length);

                                        // kick off a task to cache the result
                                        var cacheTask = CacheResizedImage(cacheFilePath, bytes);

                                        await Task.WhenAll(outputTask, cacheTask).ConfigureAwait(false);
                                    }
                                }
                            }

                        }
                    }
                }
            }
            finally
            {
                semaphore.Release();
            }
        }
Пример #21
0
        /// <summary>
        /// Asynchronously moves a specified file to a new location, providing the option to specify a new file name, and monitors cancellation requests.
        /// </summary>
        /// 
        /// <param name="sourceFileName">The name of the file to move. Can include a relative or absolute path.</param>
        /// <param name="destFileName">The new path and name for the file.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// 
        /// <remarks>
        /// <para>
        /// This method works across disk volumes, and it does not throw an exception if the source and destination are the same. 
        /// Note that if you attempt to replace a file by moving a file of the same name into that directory, you get an <see cref="IOException"/>. 
        /// You cannot use the Move method to overwrite an existing file.
        /// </para>
        /// <para>
        /// The <paramref name="sourceFileName"/> and <paramref name="destFileName"/> arguments can include relative or absolute path information. 
        /// Relative path information is interpreted as relative to the current working directory. 
        /// To obtain the current working directory, see <see cref="Directory.GetCurrentDirectory"/>.
        /// </para>
        /// <para>If you try to move a file across disk volumes and that file is in use, the file is copied to the destination, but it is not deleted from the source.</para>
        /// </remarks>
        /// 
        /// <exception cref="IOException">
        /// <para>The destination file already exists.</para>
        /// <para>-or-</para>
        /// <para><paramref name="sourceFileName"/> was not found.</para>
        /// </exception>
        /// <exception cref="ArgumentNullException"><paramref name="sourceFileName"/> or <paramref name="destFileName"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="sourceFileName"/> or <paramref name="destFileName"/> is a zero-length string, contains only white space, or contains invalid characters as defined in <see cref="Path.GetInvalidPathChars"/></exception>
        /// <exception cref="UnauthorizedAccessException">The caller does not have the required permission.</exception>
        /// <exception cref="PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.</exception>
        /// <exception cref="DirectoryNotFoundException">The path specified in <paramref name="sourceFileName"/> or <paramref name="destFileName"/> is invalid, (dor example, it is on an unmapped drive).</exception>
        /// <exception cref="NotSupportedException"><paramref name="sourceFileName"/> or <paramref name="destFileName"/> is in an invalid format.</exception>
        /// 
        /// <returns>Task that represents asynchronous operation.</returns>
        public static async Task MoveAsync(string sourceFileName, string destFileName, CancellationToken cancellationToken)
        {
            PathValidator.EnsureCorrectFileSystemPath(sourceFileName);
            PathValidator.EnsureCorrectFileSystemPath(destFileName);

            if (Path.Combine(Path.GetDirectoryName(sourceFileName), sourceFileName) == Path.Combine(Path.GetDirectoryName(destFileName), destFileName))
                return;
            const int fileBufferSize = 4096;
            using (var sourceStream = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read, fileBufferSize, true))
            {
                using (var destStream = new FileStream(destFileName, FileMode.CreateNew, FileAccess.Write, FileShare.None, fileBufferSize, true))
                {
                    const int copyBufferSize = 81920;
                    await sourceStream.CopyToAsync(destStream, copyBufferSize, cancellationToken).ConfigureAwait(false);
                    cancellationToken.ThrowIfCancellationRequested();
                }
            }
            if (!cancellationToken.IsCancellationRequested)
            {
                await DeleteAsync(sourceFileName);
            }
        }
Пример #22
0
        internal async Task<ValueTuple<SourceText, string>> ReadFileContentAsync(CommandLineSourceFile file, IList<DiagnosticInfo> diagnostics, Encoding encoding)
        {
            try
            {
                using (var data = new FileStream(file.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, bufferSize:4096, useAsync: true))
                {
                    MemoryStream stream = memoryStreamPool.Allocate();
                    stream.SetLength(0);

                    await data.CopyToAsync(stream).ConfigureAwait(false);
                    var normalizedFilePath = data.Name;
                    var text = new EncodedStringText(stream, encoding);

                    memoryStreamPool.Free(stream);

                    return ValueTuple.Create<SourceText, string>(text, normalizedFilePath);
                }

            }
            catch (Exception e)
            {
                diagnostics.Add(ToFileReadDiagostics(e, file));
                return default(ValueTuple<SourceText, string>);
            }
        }
Пример #23
0
        /// <summary>
        /// Compresses a file to another file asynchronously, using GZip at the given compression level.
        /// </summary>
        /// <param name="sourceFilePath">The source file path.</param>
        /// <param name="destinationPath">The destination path. If it doesn't exist, it will be created. If it exists, it will be replaced.</param>
        /// <param name="cancellationToken">Optional cancellation token for the task.</param>
        /// <param name="deleteSourceFileOnSuccess">If set to <c>true</c>, will delete source file if no error occurred during compression.</param>
        /// <param name="level">Compression level to use.</param>
        /// <param name="bufferSize">Size of the buffer, in bytes.</param>
        public static async Task CompressFileToGzipFileAsync( string sourceFilePath, string destinationPath, CancellationToken cancellationToken = default(CancellationToken), bool deleteSourceFileOnSuccess = true, CompressionLevel level = CompressionLevel.Optimal, int bufferSize = 64*1024 )
        {
            using( FileStream source = new FileStream( sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize, FileOptions.Asynchronous|FileOptions.SequentialScan ) )
            {
                using( FileStream destination = new FileStream( destinationPath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan ) )
                {
                    // GZipStream writes the GZipFileGeader.
                    using( GZipStream gZipStream = new GZipStream( destination, level ) )
                    {
                        await source.CopyToAsync( gZipStream, bufferSize, cancellationToken );
                    }
                }
            }

            if( !cancellationToken.IsCancellationRequested && deleteSourceFileOnSuccess )
            {
                File.Delete( sourceFilePath );
            }
        }
Пример #24
0
        /// <summary>
        /// Asynchronously copies an existing file to a new file, and monitors cancellation requests. Overwriting a file of the same name is allowed.
        /// </summary>
        /// 
        /// <param name="sourceFileName">The file to copy.</param>
        /// <param name="destFileName">The name of the destination file. This cannot be a directory.</param>
        /// <param name="overwrite"><c>true</c> if the destination file can be overwritten; otherwise, <c>false</c>.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <remarks>
        /// <para>
        /// The <paramref name="sourceFileName"/> and <paramref name="destFileName"/> parameters can specify relative or absolute path information. 
        /// Relative path information is interpreted as relative to the current working directory. 
        /// This method does not support wildcard characters in the parameters.
        /// </para>
        /// <para>The attributes of the original file are retained in the copied file.</para>
        /// </remarks>
        /// 
        /// <exception cref="ArgumentException">
        /// <paramref name="sourceFileName"/> or <paramref name="destFileName"/> is a zero-length string, contains only white space, or contains one more invalid characters defined by the <see cref="Path.GetInvalidPathChars"/> method.
        /// <para>-or-</para>
        /// <para><paramref name="sourceFileName"/> or <paramref name="destFileName"/> specifies a directory.</para>
        /// </exception>
        /// <exception cref="ArgumentNullException">Either <paramref name="sourceFileName"/> or <paramref name="destFileName"/>is <c>null</c>.</exception>
        /// <exception cref="DirectoryNotFoundException">The path specified in <paramref name="sourceFileName"/> or <paramref name="destFileName"/> is invalid (for example, it is on an unmapped drive).</exception>
        /// <exception cref="IOException">
        /// <para><paramref name="destFileName"/> exists and <paramref name="overwrite"/> is <c>false</c>.</para>
        /// <para>-or-</para>
        /// <para>An I/O error has occurred.</para>
        /// </exception>
        /// <exception cref="PathTooLongException">
        /// The specified path, file name, or both exceed the system-defined maximum length. 
        /// For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
        /// </exception>
        /// <exception cref="FileNotFoundException"><paramref name="sourceFileName"/> was not found.</exception>
        /// <exception cref="SecurityException">The caller does not have the required permission.</exception>
        /// <exception cref="UnauthorizedAccessException"><paramref name="destFileName"/> is read-only.
        /// <para>-or-</para>
        /// <para>The caller does not have the required permission.</para>
        /// </exception>
        /// 
        /// <returns>Task that represents asynchronous operation.</returns>
        public static async Task CopyAsync(string sourceFileName, string destFileName, bool overwrite, CancellationToken cancellationToken)
        {
            PathValidator.EnsureCorrectFileSystemPath(sourceFileName);
            PathValidator.EnsureCorrectFileSystemPath(destFileName);

            const int fileBufferSize = 4096;
            using (var sourceStream = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read, fileBufferSize, true))
            {
                var fileMode = overwrite ? FileMode.OpenOrCreate : FileMode.CreateNew;
                using (var destStream = new FileStream(destFileName, fileMode, FileAccess.Write, FileShare.None, fileBufferSize, true))
                {
                    const int copyBufferSize = 81920;
                    await sourceStream.CopyToAsync(destStream, copyBufferSize, cancellationToken).ConfigureAwait(false);
                    cancellationToken.ThrowIfCancellationRequested();
                }
            }
        }
Пример #25
0
 private async void generatePackage_Click(object sender, EventArgs e)
 {
     using (var ms = new MemoryStream())
     using (var fs = new FileStream(outFile.Text, FileMode.Create, FileAccess.ReadWrite))
     {
         var tlen = inFile.Text.Length;
         if (!inFile.Text.EndsWith("\\"))
         {
             tlen++;
         }
         using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
         {
             foreach (var file in Directory.GetFiles(inFile.Text, "*", SearchOption.AllDirectories))
             {
                 var relativePath = file.Substring(tlen);
                 var entry = archive.CreateEntry(relativePath);
                 using (var estream = entry.Open())
                 using (var ifs = new FileStream(file, FileMode.Open, FileAccess.Read))
                 {
                     await ifs.CopyToAsync(estream);
                 }
             }
         }
         // reset seek point of destination stream
         ms.Seek(0, SeekOrigin.Begin);
         Cryptography.Signature(ms, fs, File.ReadAllText(keyFile.Text));
     }
     MessageBox.Show("completed.", "Krile archive generator", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
Пример #26
0
		/// <summary>
		/// Create a thumbnail from a specified file.
		/// </summary>
		/// <param name="localPath">Local file path</param>
		/// <returns>BitmapImage of thumbnail</returns>
		internal static async Task<BitmapImage> CreateThumbnailAsync(string localPath)
		{
			if (String.IsNullOrWhiteSpace(localPath))
				throw new ArgumentNullException("localPath");

			if (!File.Exists(localPath))
				throw new FileNotFoundException("File seems missing.", localPath);

			try
			{
				using (var fs = new FileStream(localPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
				using (var ms = new MemoryStream())
				{
					await fs.CopyToAsync(ms).ConfigureAwait(false);

					// If continued by ContinueWith, an exception will not be caught by try-catch block in debug mode.
					return CreateThumbnailFromImageUniform(ms);
				}
			}
			catch (Exception ex)
			{
				Debug.WriteLine("Failed to create a thumbnail. {0}", ex);

				if (IsImageNotSupported(ex))
					throw new ImageNotSupportedException();

				throw;
			}
		}
Пример #27
0
        public async Task AssociateDiskAsync(DiskViewModel diskView)
        {
            DiskRecord resp;
            try
            {
                resp = await _serviceClient.GetAsync(new GetDiskInfo { DiskName = diskView.Name });
            }
            catch (WebServiceException e)
            {
                if (e.StatusCode == 404)
                {
                    resp = null;
                }
                else
                {
                    throw;
                }
            }

            var capacity = diskView.Disk.Capacity;
            diskView.SynchronizingDisk.ServerAssociation = diskView.Name;
            var oldNotificationDispatcher = diskView.Disk.NotificationDispatcher;

            if (resp == null)
            {
                // need to upload the disk first
                StatusText = String.Format("Disk {0} is being uploaded to the server. Please wait...", diskView.Name);
                try
                {
                    diskView.SynchronizingDisk.NotifySynchronized();
                    diskView.Disk.Dispose();
                    // ReSharper disable AssignNullToNotNullAttribute
                    diskView.Disk = null;
                    // ReSharper restore AssignNullToNotNullAttribute

                    using (var fs = new FileStream(diskView.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        var request = new UploadDisk
                            {
                                Capacity = capacity,
                                DiskName = diskView.Name,
                                RequestStream = fs
                            };
                        var requestUrl = String.Format("{0}{1}?Capacity={2}", _serverUrl,
                            request.ToUrl("PUT").Substring(1), capacity);

                        //resp = await _serviceClient.PutAsync(request);
                        var req = WebRequest.CreateHttp(requestUrl);
                        req.Method = "PUT";
                        req.Accept = "application/json";

                        var reqStr = await req.GetRequestStreamAsync();
                        await fs.CopyToAsync(reqStr);
                        var rawResp = await req.GetResponseAsync();
                        var responseStream = rawResp.GetResponseStream();
                        if (responseStream != null)
                        {
                            using (var respReader = new StreamReader(responseStream, Encoding.UTF8))
                            {
                                var s = new JsonSerializer<DiskRecord>();
                                resp = s.DeserializeFromReader(respReader);
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException("Missing response from upload.");
                        }

                        StatusText = string.Format("Disk {0} successfully uploaded to server.", diskView.Name);
                        await RefreshServerDisksAsync(suppressStatusMessage: true);
                    }
                }
                finally
                {
                    diskView.Disk = VirtualDisk.OpenExisting(diskView.FileName);
                    diskView.Disk.NotificationDispatcher = oldNotificationDispatcher;
                }
            }
            StatusText = String.Format("Disk {0} associated with server as {1}.", diskView.Name, resp.Name);
        }
Пример #28
0
        /// <summary>
        /// Creates a video stream attachment (upload to http://www.screen9.com/).
        /// </summary>
        /// <param name="video">The attachment.</param>
        /// <param name="path">The path to the attachment file.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public async Task CreateStreamingVideoAsync(StreamingVideo video, string path)
        {
            // 1. Request a ticket for upload
            var req = Connection.CreateRequest("videouploadticket");
            var videoUploadTicket = await ExecuteAsync<VideoUploadTicket>(Connection, req);

            // 2. Upload your video to screen9
            var uploadClient = new RestClient(videoUploadTicket.UploadUrl);
            if (Connection.HttpFactory != null) uploadClient.HttpFactory = Connection.HttpFactory;
            req = new RestRequest();

            req.AddParameter("auth", videoUploadTicket.Auth);

            var fileName = Path.GetFileName(path);
            byte[] bytes = null;

            using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
            using (var ms = new MemoryStream())
            {
                await fs.CopyToAsync(ms);
                bytes = ms.ToArray();
            }

            req.AddFile("videofile", bytes, fileName, "application/octet-stream");

            var resp = await uploadClient.ExecutePostTaskAsync(req);
            if (resp.ErrorException != null) throw new IS24Exception(string.Format("Error uploading video {0}.", path), resp.ErrorException);
            if ((int)resp.StatusCode >= 400) throw new IS24Exception(string.Format("Error uploading video {0}: {1}", path, resp.StatusDescription));

            // 3. Post StreamingVideo attachment
            req = Connection.CreateRequest("realestate/{id}/attachment", Method.POST);
            req.AddParameter("id", RealEstate.Id, ParameterType.UrlSegment);
            video.VideoId = videoUploadTicket.VideoId;
            req.AddBody(video, typeof(Attachment));

            var msg = await ExecuteAsync<Messages>(Connection, req);
            var id = msg.ExtractCreatedResourceId();

            if (!id.HasValue)
            {
                throw new IS24Exception(string.Format("Error creating attachment {0}: {1}", path, msg.ToMessage())) { Messages = msg };
            }

            video.Id = id.Value;
        }
Пример #29
0
        public static async Task<MemoryStream> HitAsync(string url)
        {
            if (!Directory.Exists(AppCacheDirectory))
            {
                Directory.CreateDirectory(AppCacheDirectory);
            }
            var uri = new Uri(url);
            var fileNameBuilder = new StringBuilder();
            using (var sha1 = new SHA1Managed())
            {
                var canonicalUrl = uri.ToString();
                var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(canonicalUrl));
                fileNameBuilder.Append(BitConverter.ToString(hash).Replace("-", "").ToLower());
                if (Path.HasExtension(canonicalUrl))
                    fileNameBuilder.Append(Path.GetExtension(canonicalUrl));
            }

            var fileName = fileNameBuilder.ToString();
            var localFile = string.Format("{0}\\{1}", AppCacheDirectory, fileName);
            var memoryStream = new MemoryStream();

            FileStream fileStream = null;
            if (!IsWritingFile.ContainsKey(fileName) && File.Exists(localFile))
            {
                using (fileStream = new FileStream(localFile, FileMode.Open, FileAccess.Read))
                {
                    await fileStream.CopyToAsync(memoryStream);
                }
                memoryStream.Seek(0, SeekOrigin.Begin);
                return memoryStream;
            }

            var request = WebRequest.Create(uri);
            request.Timeout = 30;
            try
            {
                var response = await request.GetResponseAsync();
                var responseStream = response.GetResponseStream();
                if (responseStream == null)
                    return null;
                if (!IsWritingFile.ContainsKey(fileName))
                {
                    IsWritingFile[fileName] = true;
                    fileStream = new FileStream(localFile, FileMode.Create, FileAccess.Write);
                }

                using (responseStream)
                {
                    var bytebuffer = new byte[100];
                    int bytesRead;
                    do
                    {
                        bytesRead = await responseStream.ReadAsync(bytebuffer, 0, 100);
                        if (fileStream != null)
                            await fileStream.WriteAsync(bytebuffer, 0, bytesRead);
                        await memoryStream.WriteAsync(bytebuffer, 0, bytesRead);
                    } while (bytesRead > 0);
                    if (fileStream != null)
                    {
                        await fileStream.FlushAsync();
                        fileStream.Dispose();
                        IsWritingFile.Remove(fileName);
                    }
                }
                memoryStream.Seek(0, SeekOrigin.Begin);
                return memoryStream;
            }
            catch (WebException)
            {
                return null;
            }
        }
        /// <summary>
        ///     Retrieves the binary data of the image from the cache.
        /// </summary>
        /// <param name="item">
        ///     The record that identifies the image in the cache.
        /// </param>
        /// <returns>
        ///     The binary data of the image.
        /// </returns>
        protected override async Task<byte[]> GetCacheDataAsync(CacheItem item)
        {
            byte[] imageData = null;
            var filePath = GetCacheFilePath(item);

            try
            {
                using (var memoryStream = new MemoryStream())
                {
                    using (var fileStream = new FileStream(filePath, FileMode.Open))
                    {
                        await fileStream.CopyToAsync(memoryStream);
                    }

                    if (memoryStream.Length > 0)
                    {
                        imageData = memoryStream.ToArray();
                    }
                    else
                    {
                        TryDeleteFile(filePath);
                    }
                }
            }
            catch (IOException)
            {
                // Doesn't metter
            }
            catch (SecurityException)
            {
                // Doesn't metter
            }
            catch (UnauthorizedAccessException)
            {
                // Doesn't metter
            }

            return imageData;
        }
Пример #31
0
 public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
 {
     return(tempStream.CopyToAsync(destination, bufferSize, cancellationToken));
 }
Пример #32
-1
 public async void PushFile(WebSocket client, string filePath)
 {
     using (var messageWriter = client.CreateMessageWriter(WebSocketMessageType.Binary))
     using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
     {
         await fs.CopyToAsync(messageWriter);
     }
 }