Beispiel #1
0
        public async Task <JsonResult> UploadAsync(FullPath path, IEnumerable <IFormFile> files, bool?overwrite, IEnumerable <FullPath> uploadPaths, IEnumerable <string> renames, string suffix)
        {
            var response = new AddResponseModel();

            var fileList = files.ToList();

            // Check if max upload size is set and that no files exceeds it
            if (path.RootVolume.MaxUploadSize.HasValue && fileList.Any(x => x.Length > path.RootVolume.MaxUploadSize))
            {
                // Max upload size exceeded
                return(Error.MaxUploadFileSize());
            }

            foreach (var rename in renames)
            {
                var fileInfo    = new FileInfo(Path.Combine(path.Directory.FullName, rename));
                var destination = Path.Combine(path.Directory.FullName, $"{Path.GetFileNameWithoutExtension(rename)}{suffix}{Path.GetExtension(rename)}");
                fileInfo.MoveTo(destination);
                response.Added.Add(await CustomBaseModel.CustomCreateAsync(new AzureBlobFile(destination), path.RootVolume));
            }

            var uploadPathList = uploadPaths.ToList();

            foreach (var uploadPath in uploadPathList)
            {
                var dir = uploadPath.Directory;

                while (dir.FullName != path.RootVolume.RootDirectory)
                {
                    response.Added.Add(await BaseModel.CreateAsync(new AzureBlobDirectory(dir.FullName), path.RootVolume));
                    dir = dir.Parent;
                }
            }

            var i = 0;

            foreach (var file in fileList)
            {
                var destination = uploadPathList.Count() > i?uploadPathList.ElementAt(i).Directory.FullName : path.Directory.FullName;

                var azureFile = new AzureBlobFile(AzureBlobStorageApi.PathCombine(destination, Path.GetFileName(file.FileName)));

                if (await azureFile.ExistsAsync)
                {
                    if (overwrite ?? path.RootVolume.UploadOverwrite)
                    {
                        await azureFile.DeleteAsync();

                        await AzureBlobStorageApi.UploadAsync(file, azureFile.FullName);

                        response.Added.Add(await CustomBaseModel.CustomCreateAsync(new AzureBlobFile(azureFile.FullName), path.RootVolume));
                    }
                    else
                    {
                        var newName = await CreateNameForCopy(azureFile, suffix);

                        await AzureBlobStorageApi.UploadAsync(file, AzureBlobStorageApi.PathCombine(azureFile.DirectoryName, newName));

                        response.Added.Add(await CustomBaseModel.CustomCreateAsync(new AzureBlobFile(newName), path.RootVolume));
                    }
                }
                else
                {
                    await AzureBlobStorageApi.UploadAsync(file, azureFile.FullName);

                    response.Added.Add(await CustomBaseModel.CustomCreateAsync(new AzureBlobFile(azureFile.FullName), path.RootVolume));
                }

                i++;
            }

            return(await Json(response));
        }