Ejemplo n.º 1
0
        public ActionResult <IAsyncEnumerable <File> > GetByResourceName(string resourceName, [FromQuery] string sort, [FromQuery] string from, [FromQuery] string to, [FromQuery] string minVersion, [FromQuery] string maxVersion)
        {
            try
            {
                FileSort fileSort = sort switch
                {
                    "version" => FileSort.Version,
                    "lastUpdatedTime" => FileSort.LastUpdatedTime,
                    "creationTime" => FileSort.CreationTime,
                    "name" => FileSort.Name,
                    _ => FileSort.Name
                };

                DateTime toDateTime   = DateTime.ParseExact(to, "dd-mm-yyyy", null),
                         fromDateTime = DateTime.ParseExact(from, "dd-mm-yyyy", null);
                //storageRepository.GetFilesAsync(fromDateTime, toDateTime, resourceName, minVersion, maxVersion, fileSort)

                return(Ok());
            }
            catch (Exception e)
            {
                logger.LogError($"Error in POST/files : Exception thrown with query  {Request.QueryString} and error {e.Message}");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
        /// <summary>
        /// 将分片文件合并成一个文件
        /// </summary>
        /// <param name="chunk"></param>
        /// <returns></returns>
        private async Task MergeChunkFile(FileChunk chunk)
        {
            //上传目录
            var path = AppDomain.CurrentDomain.BaseDirectory + "Files\\" + chunk.FileName;
            //分片文件命名约定
            var partToken = FileSort.PART_NUMBER;
            //上传文件的实际名称
            var baseFileName = chunk.FileName.Substring(0, chunk.FileName.IndexOf(partToken));
            //根据命名约定查询指定目录下符合条件的所有分片文件
            var searchPattern = $"{Path.GetFileName(baseFileName)}{partToken}*";
            //获取分片文件
            var fileList = Directory.GetFiles(Path.GetDirectoryName(path), searchPattern);

            if (!fileList.Any())
            {
                return;
            }

            var mergeFiles = new List <FileSort>();

            foreach (var file in fileList)
            {
                var sort = new FileSort
                {
                    FileName = file
                };
                baseFileName = file.Substring(0, file.IndexOf(partToken));
                var fileIndex = file.Substring(file.IndexOf(partToken) + partToken.Length);
                int.TryParse(fileIndex, out var number);
                sort.PartNumber = number;
                mergeFiles.Add(sort);
            }
            //排序所有分片
            mergeFiles = mergeFiles.OrderBy(x => x.PartNumber).ToList();
            //合并文件
            using (var fileStream = new FileStream(baseFileName, FileMode.Create))
            {
                foreach (var fileSort in mergeFiles)
                {
                    using (FileStream fileChunk = new FileStream(fileSort.FileName, FileMode.Open))
                    {
                        await fileChunk.CopyToAsync(fileStream);
                    }
                }
            }
            //删除分片文件
            DeleteFile(mergeFiles);
        }
Ejemplo n.º 3
0
 public static IQueryable <File> Sort(this IQueryable <File> query, FileSort sort)
 {
     if ((sort & FileSort.Name) == FileSort.Name)
     {
         query = query.OrderBy(x => x.Name);
     }
     if ((sort & FileSort.Version) == FileSort.Version)
     {
         query = query.OrderBy(x => x.Versions.Select(v => v.Version));
     }
     if ((sort & FileSort.LastUpdatedTime) == FileSort.LastUpdatedTime)
     {
         query = query.OrderBy(x => x.Versions.Select(v => v.LastUpdatedTime));
     }
     if ((sort & FileSort.CreationTime) == FileSort.CreationTime)
     {
         query = query.OrderBy(x => x.Versions.Select(v => v.CreationTime));
     }
     return(query);
 }
    public string Save(string sourceDir, string targetDir, Semaphore MaxSizeSemaphore)
    {
        try
        {
            //Folder naming
            DirectoryInfo          dir1  = new DirectoryInfo(sourceDir);
            IEnumerable <FileInfo> list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
            string dateDay    = DateTime.Now.ToString("dd");
            string dateMonth  = DateTime.Now.ToString("MM");
            string dateYear   = DateTime.Now.ToString("yyyy");
            string dateHour   = DateTime.Now.ToString("HH");
            string dateMin    = DateTime.Now.ToString("mm");
            string dateSec    = DateTime.Now.ToString("ss");
            string folderName = "\\" + dateYear + "-" + dateMonth + "-" + dateDay + "_" + dateHour + "h" + dateMin + "min" + dateSec + "-FullSave";

            string[] filesListSource = Directory.GetFiles(sourceDir, "*.*", System.IO.SearchOption.AllDirectories);
            string[] extensions      = File.ReadAllLines("ExtensionFile.txt");

            FileSort        fileSort = new FileSort();
            List <FileInfo> prioList = fileSort.PriorizeList(list1);

            TimeSpan cryptingTime;
            cryptingTime = TimeSpan.Zero;

            StateFile statefile = new StateFile();
            Save      save      = new Save();


            //If there are files to transfer
            if (filesListSource.Length != 0)
            {
                // Create the save directory
                Directory.CreateDirectory(targetDir + folderName);

                DateTime D1 = DateTime.Now;
                // Create all the Directories needed
                foreach (string dirPath in Directory.GetDirectories(sourceDir, "*", System.IO.SearchOption.AllDirectories))
                {
                    Directory.CreateDirectory(dirPath.Replace(sourceDir, targetDir + folderName));
                }


                // Copy all the files in the associated directory
                foreach (var file in prioList)
                {
                    // Remove path from the file name.
                    string fileName            = file.Name;
                    string vPath               = file.FullName.Substring(sourceDir.Length + 1);
                    bool   FileLargerParameter = false;

                    if (File.Exists("FileSizeLimit.txt"))
                    {
                        if (FileSystem.GetFileInfo($"{sourceDir}/{vPath}").Length > Convert.ToInt64(File.ReadAllText($"{Environment.CurrentDirectory}/FileSizeLimit.txt")))
                        {
                            MaxSizeSemaphore.WaitOne();
                            FileLargerParameter = true;
                        }
                    }



                    try
                    {
                        bool file2Crypt = false;
                        foreach (var ext in extensions)
                        {
                            if (ext == file.Extension)
                            {
                                file2Crypt = true;
                            }
                        }
                        if (file2Crypt == true)
                        {
                            while (Controller.EnterpriseSoftwareRunning() == true)
                            {
                            }
                            ;
                            if (file.Extension != File.ReadAllText("PriorityFiles.txt"))
                            {
                                Controller.Barrier.SignalAndWait();
                            }
                            Process processus = new Process();
                            processus.StartInfo.FileName    = Directory.GetCurrentDirectory() + "\\Cryptosoft.exe";
                            processus.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                            Console.WriteLine("\nTransfert du fichier " + fileName + "...");
                            processus.StartInfo.Arguments = "-c " + sourceDir + "\\" + vPath + " fe3a2d57c378d7dc946589e9aa8cee011cae8013 " + targetDir + folderName + "\\" + vPath;
                            processus.EnableRaisingEvents = true;
                            processus.Exited += new EventHandler(myProcess_Exited);
                            processus.Start();
                            Console.WriteLine("Cryptage du fichier" + fileName + " en cours...");
                            processus.WaitForExit();
                            cryptingTime += ((Process)processus).ExitTime - ((Process)processus).StartTime;
                        }
                        else
                        {
                            file.CopyTo(targetDir + "//" + folderName + "//" + vPath);
                        }

                        save.SaveUpdate(folderName, sourceDir, targetDir, fileName);
                        statefile.UpdateStateFile(save);
                    }

                    // Catch exception if the file was already copied.
                    catch (IOException copyError)
                    {
                        Console.WriteLine(copyError.Message);
                    }
                    if (FileLargerParameter)
                    {
                        MaxSizeSemaphore.Release();
                    }
                }
                // Calculate the transfer time
                DateTime D2           = DateTime.Now;
                TimeSpan transferTime = D2 - D1;
                Console.WriteLine("Le temps de transfert total des fichiers a été de " + transferTime);
                Console.WriteLine("Le temps de cryptage total des fichiers a été de " + cryptingTime);
                // Create and update the logs file
                LogSave logSave = new LogSave();
                logSave.CreateLog(folderName, sourceDir, targetDir, transferTime, cryptingTime);
                logSave.UpdateLogFile(logSave);
            }
            else
            {
                Console.WriteLine("No files to copy");
            }
            return(folderName);
        }
        catch (DirectoryNotFoundException dirNotFound)
        {
            Console.WriteLine(dirNotFound.Message);
            return("error");
        }
    }
        public async Task MergeChunkFile(FileChunk chunk)
        {
            var uploadDirectoryName = Path.Combine(_environment.WebRootPath, DEFAULT_FOLDER, chunk.FileName);

            var partToken = FileSort.PART_NUMBER;

            var baseFileName = chunk.FileName.Substring(0, chunk.FileName.IndexOf(partToken));

            var searchpattern = $"{Path.GetFileName(baseFileName)}{partToken}*";

            var filesList = Directory.GetFiles(Path.GetDirectoryName(uploadDirectoryName), searchpattern);

            if (!filesList.Any())
            {
                return;
            }

            var mergeFiles = new List <FileSort>();

            foreach (string file in filesList)
            {
                if (FileSingleton.Instance.InUse(baseFileName))
                {
                    continue;
                }

                FileSingleton.Instance.AddFile(file);

                if (System.IO.File.Exists(baseFileName))
                {
                    System.IO.File.Delete(baseFileName);
                }

                var sort = new FileSort
                {
                    FileName = file
                };

                baseFileName = file.Substring(0, file.IndexOf(partToken));

                var fileIndex = file.Substring(file.IndexOf(partToken) + partToken.Length);

                int.TryParse(fileIndex, out var number);
                if (number <= 0)
                {
                    continue;
                }

                sort.PartNumber = number;

                mergeFiles.Add(sort);
            }

            // 按照分片排序
            var mergeOrders = mergeFiles.OrderBy(s => s.PartNumber).ToList();

            // 合并文件
            using var fileStream = new FileStream(baseFileName, FileMode.Create);
            foreach (var fileSort in mergeOrders)
            {
                using FileStream fileChunk =
                          new FileStream(fileSort.FileName, FileMode.Open);
                await fileChunk.CopyToAsync(fileStream);
            }

            //删除分片文件
            Parallel.ForEach(mergeFiles, f =>
            {
                System.IO.File.Delete(f.FileName);
            });

            FileSingleton.Instance.RemoveFile(baseFileName);
        }