Ejemplo n.º 1
0
        /// <summary>
        /// Local upload file
        /// </summary>
        /// <param name="uploadOption">Upload option</param>
        /// <param name="fileOptions">File options</param>
        /// <param name="files">Files</param>
        /// <returns>Return the upload result</returns>
        public static async Task <UploadResult> LocalUploadAsync(UploadOption uploadOption, List <UploadFile> fileOptions, Dictionary <string, byte[]> files)
        {
            if (fileOptions.IsNullOrEmpty() || files == null || files.Count <= 0)
            {
                return(new UploadResult()
                {
                    Code = "400",
                    ErrorMessage = "No upload file configuration or file information is specified",
                    Success = false
                });
            }
            UploadResult result = new UploadResult()
            {
                Success = true,
                Files   = new List <UploadFileResult>(0)
            };

            foreach (var fileItem in files)
            {
                string fileName   = fileItem.Key;
                var    fileConfig = fileOptions.FirstOrDefault(c => c.FileName == fileName);
                if (fileConfig == null)
                {
                    fileConfig = new UploadFile()
                    {
                        FileName = fileName,
                        Rename   = false
                    };
                }
                var fileResult = await LocalUploadFileAsync(uploadOption, fileConfig, fileItem.Value).ConfigureAwait(false);

                result.Files.Add(fileResult);
            }
            return(result);
        }
Ejemplo n.º 2
0
 static UploadManager()
 {
     Default = new UploadOption()
     {
         Remote = false
     };
 }
Ejemplo n.º 3
0
        /// <summary>
        ///  Local upload file
        /// </summary>
        /// <param name="uploadOption">Upload option</param>
        /// <param name="fileOption">File option</param>
        /// <param name="file">File</param>
        /// <returns>Return the upload result</returns>
        public static async Task <UploadResult> LocalUploadAsync(UploadOption uploadOption, UploadFile fileOption, byte[] file)
        {
            var fileResult = await LocalUploadFileAsync(uploadOption, fileOption, file).ConfigureAwait(false);

            return(new UploadResult()
            {
                Code = "200",
                Success = true,
                Files = new List <UploadFileResult>()
                {
                    fileResult
                }
            });
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Upload by upload option
        /// </summary>
        /// <param name="uploadOption">Upload option</param>
        /// <param name="fileOptions">File options</param>
        /// <param name="files">Files</param>
        /// <param name="parameters">Parameters</param>
        /// <returns>Return the upload result</returns>
        public static async Task <UploadResult> UploadByOptionAsync(UploadOption uploadOption, IEnumerable <UploadFile> fileOptions, Dictionary <string, byte[]> files, Dictionary <string, string> parameters = null)
        {
            if (files == null || files.Count <= 0)
            {
                throw new ArgumentNullException(nameof(files));
            }
            if (uploadOption == null)
            {
                throw new ArgumentNullException(nameof(uploadOption));
            }
            UploadResult uploadResult = null;

            if (uploadOption.Remote)
            {
                uploadResult = await RemoteUploadAsync(uploadOption.GetRemoteOption(), fileOptions.ToList(), files, parameters).ConfigureAwait(false);
            }
            else
            {
                uploadResult = await LocalUploadAsync(uploadOption, fileOptions.ToList(), files).ConfigureAwait(false);
            }
            return(uploadResult);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Configure default upload option
 /// </summary>
 /// <param name="uploadOption">Upload option</param>
 public static void ConfigureDefault(UploadOption uploadOption)
 {
     Default = uploadOption;
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Local upload file
        /// </summary>
        /// <param name="uploadOption">Upload option</param>
        /// <param name="fileOption">File option</param>
        /// <param name="file">File</param>
        /// <returns>upload file result</returns>
        static async Task <UploadFileResult> LocalUploadFileAsync(UploadOption uploadOption, UploadFile fileOption, byte[] file)
        {
            #region verify parameters

            if (uploadOption == null)
            {
                throw new ArgumentNullException(nameof(uploadOption));
            }
            if (fileOption == null)
            {
                throw new ArgumentNullException(nameof(fileOption));
            }

            #endregion

            #region set save path

            string savePath = uploadOption.SavePath ?? string.Empty;
            if (uploadOption.SaveToContentRoot)
            {
                var contentFolder = string.IsNullOrWhiteSpace(uploadOption.ContentRootPath) ? DefaultContentFolder : uploadOption.ContentRootPath;
                savePath = Path.IsPathRooted(savePath) ? Path.Combine(savePath, contentFolder) : Path.Combine(contentFolder, savePath);
            }
            if (!string.IsNullOrWhiteSpace(fileOption.Folder))
            {
                savePath = Path.Combine(savePath, fileOption.Folder);
            }
            string realSavePath = savePath;
            if (!Path.IsPathRooted(realSavePath))
            {
                realSavePath = Path.Combine(Directory.GetCurrentDirectory(), realSavePath);
            }
            if (!Directory.Exists(realSavePath))
            {
                Directory.CreateDirectory(realSavePath);
            }

            #endregion

            #region file suffix

            string suffix = Path.GetExtension(fileOption.FileName).Trim('.');
            if (!string.IsNullOrWhiteSpace(fileOption.Suffix))
            {
                suffix = fileOption.Suffix.Trim('.');
            }

            #endregion

            #region file name

            string fileName = Path.GetFileNameWithoutExtension(fileOption.FileName);
            if (fileOption.Rename)
            {
                fileName = Guid.NewGuid().ToInt64().ToString();
            }
            fileName = string.Format("{0}.{1}", fileName, suffix);

            #endregion

            #region save file

            string fileFullPath = Path.Combine(realSavePath, fileName);
            File.WriteAllBytes(fileFullPath, file);
            string relativePath = Path.Combine(savePath ?? string.Empty, fileName);

            #endregion

            var result = new UploadFileResult()
            {
                FileName         = fileName,
                FullPath         = fileFullPath,
                Suffix           = Path.GetExtension(fileName).Trim('.'),
                RelativePath     = relativePath,
                UploadDate       = DateTimeOffset.Now,
                OriginalFileName = fileOption.FileName,
                Target           = UploadTarget.Local
            };
            return(await Task.FromResult(result).ConfigureAwait(false));
        }
Ejemplo n.º 7
0
 /// <summary>
 ///  Local upload file
 /// </summary>
 /// <param name="uploadOption">Upload option</param>
 /// <param name="fileOption">File option</param>
 /// <param name="file">File</param>
 /// <returns>Return the upload result</returns>
 public static UploadResult LocalUpload(UploadOption uploadOption, UploadFile fileOption, byte[] file)
 {
     return(LocalUploadAsync(uploadOption, fileOption, file).Result);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Local upload file
 /// </summary>
 /// <param name="uploadOption">Upload option</param>
 /// <param name="fileOptions">File options</param>
 /// <param name="files">Files</param>
 /// <returns>Return the upload result</returns>
 public static UploadResult LocalUpload(UploadOption uploadOption, List <UploadFile> fileOptions, Dictionary <string, byte[]> files)
 {
     return(LocalUploadAsync(uploadOption, fileOptions, files).Result);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Upload by upload option
 /// </summary>
 /// <param name="uploadOption">Upload option</param>
 /// <param name="fileOptions">File options</param>
 /// <param name="files">Files</param>
 /// <param name="parameters">Parameters</param>
 /// <returns>Return the upload result</returns>
 public static UploadResult UploadByOption(UploadOption uploadOption, IEnumerable <UploadFile> fileOptions, Dictionary <string, byte[]> files, Dictionary <string, string> parameters = null)
 {
     return(UploadByOptionAsync(uploadOption, fileOptions, files, parameters).Result);
 }