public IActionResult Reset(int id)
        {
            DownloadGroup group = _context.Groups.Single(g => g.ID == id);

            IEnumerable <DownloadItem> items = _context.Items.Where(i => i.DownloadGroupID == id);

            DownloadManagerService._stopRequested = true;

            foreach (DownloadItem item in items)
            {
                item.State = DownloadItem.States.Waiting;
                _context.Items.Update(item);

                try
                {
                    string filePath = System.IO.Path.Combine(_settings.DownloadFolder, group.Name, "files", item.Name);
                    System.IO.File.Delete(filePath);
                }
                catch { }
            }

            _context.SaveChanges();

            return(RedirectToAction("Show", new { id = id }));
        }
Beispiel #2
0
        public async Task MoveOther(List <DownloadItem> _items, DownloadGroup _group)
        {
            string pathFrom = System.IO.Path.Combine(_settings.DownloadFolder, _group.Name, "extracted", _items[0].GroupID.ToString());

            string[] files  = Directory.GetFiles(pathFrom);
            string   pathTo = Path.Combine(_settings.MoveFolder, "Other");

            if (!Directory.Exists(pathTo))
            {
                Directory.CreateDirectory(pathTo);
            }

            foreach (string file in files)
            {
                string fileTo = Path.Combine(pathTo, Path.GetFileName(file));
                if (File.Exists(fileTo))
                {
                    File.Delete(fileTo);
                }
                File.Move(file, fileTo);
            }

            await SocketHandler.Instance.SendIDFinish(_items[0]);

            foreach (DownloadItem item in _items)
            {
                item.State = DownloadItem.States.Finished;
            }

            _context.Items.UpdateRange(_items);
            _context.SaveChanges();
            _isMoving = false;
        }
        public IActionResult Delete(int id)
        {
            DownloadGroup group = _context.Groups.Single(g => g.ID == id);

            _context.Groups.Remove(group);

            _context.SaveChanges();
            return(RedirectToAction("List"));
        }
        public IActionResult Edit(int id)
        {
            if (!_context.Groups.Any(g => g.ID == id))
            {
                return(NotFound());
            }

            DownloadGroup group = _context.Groups.Single(g => g.ID == id);

            return(View(group));
        }
Beispiel #5
0
        public async Task MoveMovie(List <DownloadItem> _items, DownloadGroup _group)
        {
            string pathFrom = System.IO.Path.Combine(_settings.DownloadFolder, _group.Name, "extracted", _items[0].GroupID.ToString());

            string[] files = Directory.GetFiles(pathFrom, "*.mkv");

            string fileToMove = "";

            foreach (string file in files)
            {
                if (Path.GetFileName(file).Contains("sample"))
                {
                    continue;
                }

                fileToMove = file;
                break;
            }

            string pathTo = Path.Combine(_settings.MoveFolder, "Filme");
            string fileTo = Path.Combine(pathTo, _group.Sort + Path.GetExtension(fileToMove));

            if (!Directory.Exists(pathTo))
            {
                Directory.CreateDirectory(pathTo);
            }

            try
            {
                if (File.Exists(fileTo))
                {
                    File.Delete(fileTo);
                }
                File.Move(fileToMove, fileTo);
            } catch (Exception e)
            {
                Console.WriteLine("Verschieben: " + e.Message);
            }


            await SocketHandler.Instance.SendIDFinish(_items[0]);

            foreach (DownloadItem item in _items)
            {
                item.State = DownloadItem.States.Finished;
            }

            _context.Items.UpdateRange(_items);
            _context.SaveChanges();
            _isMoving = false;
        }
        public IActionResult apiGetNewItem(int id)
        {
            DownloadItem  item  = _context.Items.Single(i => i.ID == id);
            DownloadGroup group = _context.Groups.Single(g => g.ID == item.DownloadGroupID);

            Dictionary <string, string> ret = new Dictionary <string, string>();

            string url = Url.Action("Show", new { id = group.ID });

            ret.Add("group", "<a href='" + url + "'>" + group.Name + "</a>");
            ret.Add("file", item.Name);

            return(Json(ret));
        }
Beispiel #7
0
        public static DownloadGroup SortGroups(DownloadGroup group)
        {
            Regex regPart = new Regex(@"(.+)\.part[0-9]{0,4}\.rar$");
            Regex regR0   = new Regex(@"(.+)\.r[0-9]{0,4}$");

            int currentGroup = 0;

            while (true)
            {
                currentGroup += 1;
                DownloadItem tempItem = null;
                try
                {
                    tempItem = group.Items.First(i => i.GroupID == -1);
                }
                catch (Exception) { break; }

                tempItem.GroupID = currentGroup;

                Match mPart = regPart.Match(tempItem.Name);
                Match mR0   = regR0.Match(tempItem.Name);


                if (mPart.Success)
                {
                    string partname = mPart.Groups[1].Value;

                    foreach (DownloadItem toEdit in group.Items.Where(i => i.Name.StartsWith(partname)))
                    {
                        toEdit.GroupID = currentGroup;
                    }
                }

                if (mR0.Success)
                {
                    string partname = mR0.Groups[1].Value;

                    foreach (DownloadItem toEdit in group.Items.Where(i => i.Name.StartsWith(partname)))
                    {
                        toEdit.GroupID = currentGroup;
                    }
                }
            }

            return(group);
        }
        public async Task <IActionResult> AddDlcConfirm(string[] items, AddDlcModel model)
        {
            DownloadGroup group = new DownloadGroup();

            group.Name     = model.Name;
            group.Password = model.Password;
            group.Type     = model.Type;
            group.Sort     = model.nameToSort;

            if (items.Count() < 1)
            {
                throw new Exception("No Items selected/found");
            }

            IDownloader down;

            foreach (string itemStr in items)
            {
                DownloadItem item = new DownloadItem()
                {
                    Url = itemStr
                };

                down         = DownloadHelper.GetDownloader(itemStr);
                item.Hoster  = down.Identifier;
                item.ShareId = down.GetItemId(itemStr);
                item         = await down.GetItemInfo(item);

                group.Items.Add(item);
            }

            group = DownloadHelper.SortGroups(group);
            _context.Groups.Add(group);
            _context.SaveChanges();


            foreach (DownloadItem item in group.Items)
            {
                item.DownloadGroupID = group.ID;
                _context.Items.Add(item);
            }
            _context.SaveChanges();


            return(RedirectToAction("Show", new { id = group.ID }));
        }
Beispiel #9
0
        public IActionResult Reset(int id)
        {
            DownloadItem  item  = _context.Items.Single(i => i.ID == id);
            DownloadGroup group = _context.Groups.Single(g => g.ID == item.DownloadGroupID);

            string filePath = System.IO.Path.Combine(_settings.DownloadFolder, group.Name, "files", item.Name);

            try
            {
                System.IO.File.Delete(filePath);
            }
            catch { }

            SocketHandler.Instance.SendIDReset(item);

            item.State = DownloadItem.States.Waiting;
            _context.Update(item);
            _context.SaveChanges();

            return(RedirectToAction("Show", "Downloads", new { id = group.ID }));
        }
Beispiel #10
0
        public async Task DoMove(List <DownloadItem> _items, DownloadGroup _group)
        {
            switch (_group.Type)
            {
            case DownloadType.Movie:
                await MoveMovie(_items, _group);

                break;

            case DownloadType.Soap:
                await MoveSoap(_items, _group);

                break;

            case DownloadType.Other:
                await MoveOther(_items, _group);

                break;
            }

            _isMoving = false;
        }
Beispiel #11
0
        public async static Task <AddDlcModel> ParseDlc(Stream dlcStream)
        {
            StreamReader s       = new StreamReader(dlcStream);
            string       content = await s.ReadToEndAsync();

            s.Dispose();

            string dlc_key  = content.Substring(content.Length - 88);
            string dlc_data = content.Substring(0, content.Length - 88);

            HttpClient client = new HttpClient();
            string     resp   = "";

            try
            {
                resp = await client.GetStringAsync("http://service.jdownloader.org/dlcrypt/service.php?srcType=dlc&destType=pylo&data=" + dlc_key);
            } catch
            {
                return(null);
            }

            string dlc_enc_key = resp.Substring(4, resp.Length - 9);
            string dlc_dec_key = Decrypt(dlc_enc_key);

            string links_enc = Decrypt(dlc_data, dlc_dec_key).Replace("\0", "");
            string links_dec = UTF8Encoding.Default.GetString(Convert.FromBase64String(links_enc));

            XDocument     xml      = XDocument.Parse(links_dec);
            XElement      package  = xml.Element("dlc").Element("content").Element("package");
            UTF8Encoding  encoding = new UTF8Encoding();
            DownloadGroup group    = new DownloadGroup();
            IDownloader   downloader;

            foreach (XElement file in package.Elements("file"))
            {
                string fileUrl = encoding.GetString(Convert.FromBase64String(file.Element("url").Value));
                downloader = DownloadHelper.GetDownloader(fileUrl);

                if (downloader == null)
                {
                    continue;
                }

                DownloadItem item = new DownloadItem();
                item.Name    = encoding.GetString(Convert.FromBase64String(file.Element("filename").Value));
                item.ShareId = downloader.GetItemId(fileUrl);
                item.Hoster  = downloader.Identifier;
                item.Url     = fileUrl;

                item = await downloader.GetItemInfo(item);



                group.Items.Add(item);
            }

            group = DownloadHelper.SortGroups(group);

            AddDlcModel output = new AddDlcModel();

            output.Name = package.Attribute("name") != null?encoding.GetString(Convert.FromBase64String(package.Attribute("name").Value)).Trim() : "";

            output.Password = "";

            foreach (DownloadItem item in group.Items)
            {
                if (!output.Items.ContainsKey(item.GroupID))
                {
                    output.Items.Add(item.GroupID, new AddDlcGroupModel());
                }

                output.Items[item.GroupID].Items.Add(item);
            }


            return(output);
        }
Beispiel #12
0
        public async Task MoveSoap(List <DownloadItem> _items, DownloadGroup _group)
        {
            bool dyn = (_group.Sort.Substring(_group.Sort.LastIndexOf(" ") + 1)) == "dynamisch";

            if (dyn)
            {
                _group.Sort = _group.Sort.Substring(0, _group.Sort.LastIndexOf("/"));
            }
            else
            {
                _group.Sort = _group.Sort.Replace('/', Path.DirectorySeparatorChar);
            }

            string pathFrom = System.IO.Path.Combine(_settings.DownloadFolder, _group.Name, "extracted", _items[0].GroupID.ToString());

            string[] files = Directory.GetFiles(pathFrom, "*.mkv");


            foreach (string file in files)
            {
                string filename = Path.GetFileName(file);
                if (filename.Contains("sample"))
                {
                    continue;
                }

                string pathTo = "";

                if (dyn)
                {
                    Regex reg = new Regex(@"(?i)s([0-9]{1,2})[\.]{0,1}e[0-9]{1,2}");
                    Match m   = reg.Match(filename);
                    pathTo = Path.Combine(_settings.MoveFolder, "Serien", _group.Sort, "Staffel " + m.Groups[1].Value);
                }
                else
                {
                    pathTo = Path.Combine(_settings.MoveFolder, "Serien", _group.Sort);
                }

                if (!Directory.Exists(pathTo))
                {
                    Directory.CreateDirectory(pathTo);
                }

                string endpath = Path.Combine(pathTo, filename);

                try
                {
                    if (File.Exists(endpath))
                    {
                        File.Delete(endpath);
                    }
                    File.Move(file, endpath);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Verschieben: " + e.Message);
                }
            }

            await SocketHandler.Instance.SendIDFinish(_items[0]);

            foreach (DownloadItem item in _items)
            {
                item.State = DownloadItem.States.Finished;
            }

            _context.Items.UpdateRange(_items);
            _context.SaveChanges();

            _isMoving = false;
        }
        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;
        }
 public IActionResult Edit(DownloadGroup group)
 {
     _context.Groups.Update(group);
     _context.SaveChanges();
     return(RedirectToAction("Show", new { id = group.ID }));
 }
Beispiel #15
0
        private async Task StartDownload(DownloadGroup group, DownloadItem item, AccountProfile profile)
        {
            Log("State Downloading - " + item.ID.ToString() + " - " + item.Name);
            item.State = DownloadItem.States.Downloading;
            queryUpdate.Add(item);

            Log("Start Download " + item.ID);
            _lastReadList = new List <double>();
            _lastCurrent  = 0;

            System.IO.Stream s = null;

            IDownloader downloader = DownloadHelper.GetDownloader(item);

            try
            {
                s = await downloader.GetDownloadStream(item, profile);
            }
            catch (Exception e)
            {
                Log("State Error - " + item.ID.ToString() + " - " + item.Name);
                item.State = DownloadItem.States.Error;
                queryUpdate.Add(item);
                queryAdd.Add(new DownloadError(3, item, e));
                await SocketHandler.Instance.SendIDError(item);

                _isDownloading = false;
                Log("Stream Problem");
                Log("DOWNLOAD FALSE");
                return;
            }
            Log("Got Stream " + item.ID);

            string groupDir   = System.IO.Path.Combine(_settings.DownloadFolder, group.Name);
            string filesDir   = System.IO.Path.Combine(_settings.DownloadFolder, group.Name, "files");
            string extractDir = System.IO.Path.Combine(_settings.DownloadFolder, group.Name, "extracted");

            try
            {
                if (!System.IO.Directory.Exists(groupDir))
                {
                    System.IO.Directory.CreateDirectory(groupDir);
                }

                if (!System.IO.Directory.Exists(filesDir))
                {
                    System.IO.Directory.CreateDirectory(filesDir);
                }

                if (!System.IO.Directory.Exists(extractDir))
                {
                    System.IO.Directory.CreateDirectory(extractDir);
                }
            }
            catch (Exception e)
            {
                Log("State Error - " + item.ID.ToString() + " - " + item.Name);
                item.State = DownloadItem.States.Error;
                queryUpdate.Add(item);
                await SocketHandler.Instance.SendIDError(item);

                queryAdd.Add(new DownloadError(4, item, e));
                Log("Error happened: " + e.Message);
                _isDownloading = false;
                return;
            }
            Log("Folders Checked " + item.ID + " - " + item.Name);


            string filePath = System.IO.Path.Combine(filesDir, item.Name);

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

            System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate);

            _state = new Timer(CheckStatus, new GroupItemModel()
            {
                Group = group, Item = item
            }, 0, 1000);
            Log("Timer started " + item.ID);

            try
            {
                s.CopyTo(fs);
            }
            catch (Exception e)
            {
                await SocketHandler.Instance.SendIDError(item);

                item.State = DownloadItem.States.Error;
                Log("State Error - " + item.ID.ToString() + " - " + item.Name);
                queryUpdate.Add(item);
                queryAdd.Add(new DownloadError(5, item, e));
                _isDownloading = false;
                return;
            }

            fs.Close();
            fs.Dispose();
            _state.Dispose();
            _state = null;
            Log("fs + state disposed");



            if (_stopRequested)
            {
                _stopRequested = false;
                Log("Cancel requested");
                item.State = DownloadItem.States.Waiting;
                Log("State Waiting - " + item.ID.ToString() + " - " + item.Name);
                queryUpdate.Add(item);
                _isDownloading = false;
                return;
            }

            Log("Download finished " + item.ID);

            if (item.MD5 == null)
            {
                await SocketHandler.Instance.SendIDDownloaded(item);

                Log("State Downloaded - " + item.ID.ToString() + " - " + item.Name);
                item.State = DownloadItem.States.Downloaded;
                Log("Md5 not set " + item.ID);
                queryUpdate.Add(item);
                _isDownloading = false;

                return;
            }


            await SocketHandler.Instance.SendIDCheck(item);

            Log("Check MD5 " + item.ID);
            string hash = "";

            using (var md5 = MD5.Create())
                using (var stream = System.IO.File.OpenRead(filesDir + System.IO.Path.DirectorySeparatorChar + item.Name))
                    hash = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", string.Empty).ToLowerInvariant();

            if (item.MD5 == hash)
            {
                await SocketHandler.Instance.SendIDDownloaded(item);

                item.State = DownloadItem.States.Downloaded;
            }
            else
            {
                await SocketHandler.Instance.SendIDError(item);

                item.State = DownloadItem.States.Error;
                Log("MD5 Error - " + item.ID.ToString() + " - " + item.Name);
                queryAdd.Add(new DownloadError(6, item, new Exception("MD5 Check failed")));
            }

            Log("Md5 checked " + item.ID);

            try
            {
                queryUpdate.Add(item);
            }
            catch (Exception e)
            {
                Log("Update Error - " + e.Message + " - " + item.Name);
            }
            _isDownloading = false;

            Log("Added to query download completed " + item.ID);
        }