Example #1
0
 /// <summary>
 /// Write byte array to a file. See: https://github.com/FrendsPlatform/Frends.File#WriteBytes
 /// </summary>
 /// <returns>Object {string Path, double SizeInMegaBytes}</returns>
 public static async Task <WriteResult> WriteBytes([PropertyTab] WriteBytesInput input, [PropertyTab] WriteBytesOption options)
 {
     return(await ExecuteActionAsync(
                () => ExecuteWriteBytes(input, options),
                options.UseGivenUserCredentialsForRemoteConnections,
                options.UserName,
                options.Password)
            .ConfigureAwait(false));
 }
Example #2
0
        private static async Task <WriteResult> ExecuteWriteBytes(WriteBytesInput input, WriteBytesOption options)
        {
            var bytes = input?.ContentBytes as byte[]; // TODO: Use corrctly typed input once UI support expression default editor for arrays

            if (bytes == null)
            {
                throw new ArgumentException("Input.ContentBytes must be a byte array", nameof(input.ContentBytes));
            }

            var fileMode = GetAndCheckWriteMode(options.WriteBehaviour, input.Path);

            using (var fileStream = new FileStream(input.Path, fileMode, FileAccess.Write, FileShare.Write, 4096, useAsync: true))
            {
                var memoryStream = new MemoryStream(bytes);
                await memoryStream.CopyToAsync(fileStream).ConfigureAwait(false);
            }

            return(new WriteResult(new FileInfo(input.Path)));
        }