예제 #1
0
        public async Task <IActionResult> Create([Bind("Id,File,Modified,SizeKb,ProductName")] Download download)
        {
            if (ModelState.IsValid)
            {
                _context.Add(download);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(download));
        }
        public async Task DoExtract(List <DownloadItem> itemsGroup, DownloadGroup group)
        {
            DownloadItem item = itemsGroup.OrderBy(i => i.Name).First();

            string firstPart = item.Name;

            //TODO check if neccessary

            Regex reg = new Regex(@"\.part[0]{0,}1\.rar");
            Match m   = reg.Match(item.Name);

            if (!m.Success && itemsGroup.Count > 1)
            {
                reg = new Regex(@"(.+)\.r[0-9]{0,4}$");
                if (reg.IsMatch(itemsGroup[1].Name))
                {
                    foreach (DownloadItem ti in itemsGroup)
                    {
                        if (ti.Name.EndsWith(".rar"))
                        {
                            firstPart = ti.Name;
                        }
                    }
                }
            }

            foreach (DownloadItem i in itemsGroup)
            {
                await SocketHandler.Instance.SendIDExtract(i);

                i.State = DownloadItem.States.Extracting;
                _context.Items.Update(i);
            }
            await _context.SaveChangesAsync();

            string fileDir    = Path.Combine(_settings.DownloadFolder, group.Name, "files") + Path.DirectorySeparatorChar + firstPart;
            string extractDir = Path.Combine(_settings.DownloadFolder, group.Name, "extracted", item.GroupID.ToString());
            string args       = _settings.ZipArgs.Replace("%filesDir%", fileDir).Replace("%extractDir%", extractDir).Replace("%pass%", group.Password);

            if (!string.IsNullOrEmpty(group.Password))
            {
                args += " -p\"" + group.Password + "\""; //TODO remove
            }
            Process p = new Process();

            p.StartInfo.FileName  = _settings.ZipPath;
            p.StartInfo.Arguments = args;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError  = true;
            p.Start();


            StartOutStandard(p, Path.Combine(_settings.LogFolder, group.Name, "log-" + item.GroupID.ToString() + "-extract.log"), item);

            p.WaitForExit();

            if (p.ExitCode != 0)
            {
                bool flag = false;
                foreach (string line in _standardOutput)
                {
                    if (line.Contains("ERROR"))
                    {
                        DownloadError error = new DownloadError(7, item, new Exception(line));
                        _context.Errors.Add(error);
                        flag = true;
                    }
                }

                if (!flag)
                {
                    DownloadError error = new DownloadError(8, item, new Exception(string.Join(Environment.NewLine, _standardOutput)));
                    _context.Errors.Add(error);
                }
                await SocketHandler.Instance.SendIDError(item);

                item.State = DownloadItem.States.Error;
                _context.Items.Update(item);
                foreach (DownloadItem i in itemsGroup)
                {
                    if (i == item)
                    {
                        continue;
                    }
                    i.State = DownloadItem.States.Downloaded;
                    _context.Items.Update(i);
                }
            }
            else
            {
                await SocketHandler.Instance.SendIDExtracted(item);

                foreach (DownloadItem i in itemsGroup)
                {
                    i.State = DownloadItem.States.Extracted;
                    _context.Items.Update(i);
                    //Todo delete files
                    System.IO.File.Delete(Path.Combine(_settings.DownloadFolder, group.Name, "files", i.Name));
                }
            }

            await _context.SaveChangesAsync();

            isExtracting = false;
        }