public static StorageCommand SetStreamSource(this StorageCommand command, Stream stream, string fileExtension)
        {
            var source = StorageCommand.CreateSourceProvider(async () => stream, false);
            command.Extension = fileExtension;

            return command.SetSourceProvider(source);
        }
예제 #2
0
        /// <summary>
        /// Save a file from various sources to the remote storage
        /// </summary>
        /// <param name="command">Options for the saving the file</param>
        /// <returns>name of file as stored in the remote storage</returns>
        public async Task <string> SaveAsync(StorageCommand command)
        {
            var    sourceProvider = command.SourceProvider;
            Stream sourceStream   = null;

            try
            {
                sourceStream = await sourceProvider.GetStreamAsync().IgnoreContext();

                if (String.IsNullOrEmpty(command.FileName))
                {
                    command.FileName = String.Format("{0}{1}", Guid.NewGuid().ToShortGuid(), command.Extension);
                }
                else
                {
                    if (!String.IsNullOrEmpty(command.Extension) && String.IsNullOrEmpty(Path.GetExtension(command.FileName)))
                    {
                        command.FileName = $"{command.FileName}{command.Extension}";
                    }
                }

                if (String.IsNullOrEmpty(command.ContentType))
                {
                    command.ContentType = GlobalConfiguration.Instance.GetMimeMapping().GetMimeType(command.FileName);
                }

                if (!String.IsNullOrEmpty(command.Folder))
                {
                    command.FileName = "{0}/{1}".FormatWith(command.Folder, command.FileName);
                }

                var keys = command.EncryptionOptions?.GetKeys(command.FileName);
                if (keys != null)
                {
                    using (var streamToSave = await EncryptedStream.CreateAsync(sourceStream, keys.Secret, keys.Salt)
                                              .IgnoreContext())
                    {
                        await _storage.SaveAsync(streamToSave, command.FileName, command.ContentType)
                        .IgnoreContext();
                    }
                }
                else
                {
                    await _storage.SaveAsync(sourceStream, command.FileName, command.ContentType)
                    .IgnoreContext();
                }
            }
            finally
            {
                if (sourceStream != null && sourceProvider.AutoDispose)
                {
                    sourceStream.Dispose();
                }
            }

            return(command.FileName);
        }
        public static StorageCommand SetUrlSource(this StorageCommand command, string url)
        {
            var source = StorageCommand.CreateSourceProvider(async () =>
            {
                var downloaded = await DownloadHelper.GetDataAsync(url);
                if (!downloaded.HasOkStatus())
                {
                    throw new ApplicationException($"Error downloading data at '{url}'", downloaded.Exception);
                }
                command.ContentType = command.ContentType.DefaultTo(downloaded.ContentType);
                var fileName = $"{Guid.NewGuid().ToShortGuid()}{Path.GetExtension(new Uri(url).AbsolutePath)}";
                command.FileName = command.FileName.DefaultTo(fileName);
                return new MemoryStream(downloaded.Data);
            }, true);

            return command.SetSourceProvider(source);
        }
        public static StorageCommand SetImageSource(this StorageCommand command, Image image)
        {
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
            var source = StorageCommand.CreateSourceProvider(async () =>
            {
                ImageFormat format = null;
                if (!String.IsNullOrEmpty(command.Extension))
                {
                    format = command.Extension.GetImageFormat();
                }
                else
                {
                    format = image.GetImageFormat();
                    command.Extension = format.FileExtension();
                }
                return image.ToStream(format);
            }, true);
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously

            return command.SetSourceProvider(source);
        }
        public static StorageCommand SetFilePathSource(this StorageCommand command, string filePath)
        {
            var source = StorageCommand.CreateSourceProvider(async () => new FileStream(filePath, FileMode.Open, FileAccess.Read), true);

            return command.SetSourceProvider(source);
        }
        public static StorageCommand SetByteArraySource(this StorageCommand command, byte[] byteArray)
        {
            var source = StorageCommand.CreateSourceProvider(async () => new MemoryStream(byteArray), true);

            return command.SetSourceProvider(source);
        }
        public static StorageCommand SetStreamSource(this StorageCommand command, Stream stream)
        {
            var source = StorageCommand.CreateSourceProvider(async () => stream, false);

            return command.SetSourceProvider(source);
        }
예제 #8
0
 /// <summary>
 /// Save a file from various sources to the remote storage
 /// </summary>
 /// <param name="command">Options for the saving the file</param>
 /// <returns>name of file as stored in the remote storage</returns>
 public string Save(StorageCommand command)
 {
     return(TaskHelper.GetResultSafeSync(() => SaveAsync(command)));
 }