Пример #1
0
        public string PrepareFilesDownload([FromForm] SelectFilesModel model)
        {
            string rootPath          = Path.Combine(_environment.ContentRootPath, "Uploads");
            var    decompressedFiles = new List <string>();

            //1. Create new folder
            string folderName        = Guid.NewGuid().ToString();
            string destinationFolder = Path.Combine(rootPath, folderName);

            Directory.CreateDirectory(destinationFolder);

            //2. Decompress selected files into the new folder
            foreach (var fileGuid in model.FileGuids)
            {
                var    file = _db.CajFile.FirstOrDefault(x => x.Id == fileGuid);
                string serverSourceFilePath      = Path.Combine(rootPath, file.Id.ToString());
                string serverDestinationFilePath = Path.Combine(destinationFolder, file.Name);

                //3. Rename them to the original name
                if (IOManager.Decompress(serverSourceFilePath, serverDestinationFilePath))
                {
                    decompressedFiles.Add(serverDestinationFilePath);
                }
            }

            //4. Compress them
            string serverCompressedFilePath = Path.Combine(destinationFolder, $"{folderName}.zip");

            if (IOManager.CompressFiles(decompressedFiles.ToArray(), serverCompressedFilePath))
            {
                foreach (var decompressedFile in decompressedFiles)
                {
                    IOManager.RemoveFile(decompressedFile);
                }

                //5. Return destination folder name
                return(folderName);
            }

            return(null);
        }