//data plan currently not involved with nav bars
 public ActionResult <JsonResponse> PostNavBarMethod(int admin_id, string admin_token, int site_id)
 {
     if (authenticator.VerifyAdminForLeaf(admin_id, site_id, admin_token))
     {
         dbQuery.AddNavBarToSite(site_id);
         JsonResponse r = new JsonSuccess("Nav Bar posted sucessfully!");
         return(r);
     }
     else
     {
         return(StatusCode(400, "Invalid Token. Stranger Danger."));
     }
 }
Esempio n. 2
0
        public string OpenDirectory(string directoryPath)
        {
            DebugHandler.TraceMessage("OpenDirectory Called", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage("Directory Path: " + directoryPath, DebugSource.TASK, DebugType.PARAMETERS);

            try
            {
#if __ANDROID__
                Android.Net.Uri        uri    = Android.Net.Uri.Parse(directoryPath);
                Android.Content.Intent intent = new Android.Content.Intent(Android.Content.Intent.ActionView);
                intent.SetDataAndType(uri, "*/*");
                intent.SetFlags(Android.Content.ActivityFlags.ClearWhenTaskReset | Android.Content.ActivityFlags.NewTask);
                Android.App.Application.Context.StartActivity(Android.Content.Intent.CreateChooser(intent, "Choose File Explorer"));
#else
                if (UtilityMethods.CheckOperatingSystems() == UtilityMethods.OperatingSystems.Linux)
                {
                    Process.Start("xdg-open", directoryPath);
                }
                else if (UtilityMethods.CheckOperatingSystems() == UtilityMethods.OperatingSystems.OsX)
                {
                    Process.Start("open", directoryPath);
                }
                else
                {
                    Process.Start("explorer.exe", directoryPath);
                }
#endif
                JsonSuccess report = new JsonSuccess()
                {
                    message = "Succesfully opened folder with path: " + directoryPath
                };

                return(report.ToJson());
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage("Could not open directory: " + e.ToString(), DebugSource.TASK, DebugType.WARNING);

                JsonError jsonError = new JsonError
                {
                    type         = "open_directory_failed",
                    errormessage = "Could not open directory.",
                    errortype    = "exception"
                };

                return(jsonError.ToJson());
            }
        }
Esempio n. 3
0
        public async Task <string> AbortDownload(string id = null, string filePath = null)
        {
            DebugHandler.TraceMessage("AbortDownload Called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage("ID: " + id + ", FILEPATH: " + filePath, DebugSource.TASK, DebugType.PARAMETERS);

            try
            {
                if (IrcClientHandler.IsDownloading() && DownloadProcesOnGoing)
                {
                    if (CurrentlyDownloading.id == id)
                    {
                        DebugHandler.TraceMessage("Stopping the current download!", DebugSource.TASK, DebugType.INFO);
                        IrcClientHandler.StopDownload();
                    }
                }

                while (IrcClientHandler.IsDownloading())
                {
                    DebugHandler.TraceMessage("Current download still running!", DebugSource.TASK, DebugType.INFO);
                    await Task.Delay(100);
                }

                DebugHandler.TraceMessage("Current download stopped!", DebugSource.TASK, DebugType.INFO);
                JsonSuccess succes = new JsonSuccess()
                {
                    message = "Succesfully aborted download from download queue by download json."
                };

                return(succes.ToJson());
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage("Could not remove download from queue by download json and stop the download.", DebugSource.TASK, DebugType.WARNING);
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);
                JsonError error = new JsonError()
                {
                    type         = "remove_download_error",
                    errormessage = "Could not remove download from queue by download json.",
                    errortype    = "exception"
                };
                return(error.ToJson());
            }
        }
        public ActionResult <JsonResponse> DeleteSite(int site_id, int admin_id, string admin_token)
        {
            if (authenticator.VerifyAdminForLeaf(admin_id, site_id, admin_token))
            {
                SkeletonSiteDto found_site = dbQuery.QuerySkeletonSiteById(site_id);

                //must be done manually by the app to properly update the data limiter
                foreach (SiteComponentDto site_component in found_site.site_components)
                {
                    ComponentReference component_reference = new ComponentReference()
                    {
                        component_id   = site_component.component_id,
                        component_type = site_component.type
                    };
                    DeleteAuthenticatedSiteComponentMethod(component_reference);
                }

                //manually remove nav link data from data plan
                List <NavLink> found_nav_links;
                try{
                    found_nav_links = dbQuery.QueryNavBarLinksBySiteId(site_id);
                }catch {
                    found_nav_links = null;
                }
                if (found_nav_links != null)
                {
                    foreach (NavLink nav_link in found_nav_links)
                    {
                        _dataLimiter.RemoveNavLinkFromDataPlan(nav_link, admin_id);
                    }
                }

                _dataLimiter.RemoveSiteFromDataPlan(admin_id);
                Site         DeletedSite = dbQuery.DeleteSiteById(site_id);
                JsonResponse r           = new JsonSuccess($"Site {DeletedSite.title} deleted sucessfully!");
                return(r);
            }
            else
            {
                return(StatusCode(400, "Invalid Token. Stranger Danger."));
            }
        }
        public ActionResult <JsonResponse> PostLinkBoxMethod(NewLinkBoxDto _NewLinkBox, int admin_id, string admin_token)
        {
            LinkBox NewLinkBox = new LinkBox();

            NewLinkBox.title        = _NewLinkBox.title;
            NewLinkBox.content      = _NewLinkBox.content;
            NewLinkBox.url          = _NewLinkBox.url;
            NewLinkBox.link_display = _NewLinkBox.link_display;


            NewLinkBox.priority  = _NewLinkBox.priority;
            NewLinkBox.site_id   = _NewLinkBox.site_id;
            NewLinkBox.byte_size = NewLinkBox.FindCharLength();

            if (authenticator.VerifyAdminForLeaf(admin_id, NewLinkBox.site_id, admin_token))
            {
                List <string> errors = authenticator.ValidateIncomingComponent(NewLinkBox);
                if (errors.Count == 0)
                {
                    DataPlan data_plan;
                    try{
                        data_plan = _dataLimiter.ValidateComponentAdditionForDataPlan(admin_id, NewLinkBox);
                    }catch (System.ArgumentException e) {
                        return(StatusCode(400, e.Message));
                    }

                    dbQuery.AddLinkBox(NewLinkBox);
                    _dataLimiter.UpdateDataPlan(data_plan);
                    JsonResponse r = new JsonSuccess("Link Box posted sucessfully!");
                    return(r);
                }
                else
                {
                    return(StatusCode(400, errors));
                }
            }
            else
            {
                return(StatusCode(400, "Invalid Token. Stranger Danger."));
            }
        }
        public ActionResult <JsonResponse> PostMethod(NewSiteDto NewSite)
        {
            if (authenticator.VerifyAdmin(NewSite.admin_id, NewSite.token))
            {
                string verdict = validator.ValidateSiteUrl(NewSite.url);
                if (verdict == "pass")
                {
                    DataPlan data_plan;
                    try{
                        data_plan = _dataLimiter.ValidateSiteAdditionForDataPlan(NewSite.admin_id);
                    }catch (System.ArgumentException e) {
                        return(StatusCode(400, e.Message));
                    }

                    Site SoonToAddSite = new Site();
                    SoonToAddSite.title    = NewSite.title;
                    SoonToAddSite.admin_id = NewSite.admin_id;
                    SoonToAddSite.url      = NewSite.url.ToLower();
                    List <string> format_errors = authenticator.ValidateIncomingSite(SoonToAddSite);
                    if (format_errors.Count != 0)
                    {
                        return(StatusCode(400, format_errors[0]));
                    }
                    dbQuery.AddSite(SoonToAddSite);
                    _dataLimiter.UpdateDataPlan(data_plan);
                    JsonResponse r = new JsonSuccess($"Site created with title: ${NewSite.title}");
                    return(r);
                }
                else
                {
                    JsonFailure f = new JsonFailure(verdict);
                    return(StatusCode(400, f));
                }
            }
            else
            {
                return(StatusCode(400, "Invalid Token. Stranger Danger."));
            }
        }
Esempio n. 7
0
        public string AddDownload(JsonDownloadInfo download)
        {
            DebugHandler.TraceMessage("AddDownload Called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(download.ToString(), DebugSource.TASK, DebugType.PARAMETERS);
            try
            {
                if (download.filesize.Contains("."))
                {
                    download.filesize = ((int)(double.Parse(download.filesize, System.Globalization.CultureInfo.InvariantCulture) * 1024)).ToString();
                }
                if (UtilityMethods.GetFreeSpace(IrcSettings.fullfilepath) > (int.Parse(download.filesize) * 1024 * 1024))
                {
                    download.downloadIndex = DownloadQueue.Count - 1;

                    if (!DownloadQueue.Contains(download) || CurrentlyDownloading != download)
                    {
                        DownloadQueue.Add(download);
                        DebugHandler.TraceMessage("Added download to queue: " + download.ToString(), DebugSource.TASK, DebugType.INFO);

                        JsonSuccess succes = new JsonSuccess()
                        {
                            message = "Succesfully added download to download queue."
                        };

                        return(succes.ToJson());
                    }
                    else
                    {
                        DebugHandler.TraceMessage("Could not add download: " + download.ToString() + ", already exist in queue or is already being downloaded ", DebugSource.TASK, DebugType.WARNING);
                        JsonError error = new JsonError()
                        {
                            type         = "file_already_being_downloaded_error",
                            errormessage = "Could not add download: " + download.ToString() + ", already exist in queue or is already being downloaded ",
                            errortype    = "warning",
                            exception    = "none"
                        };
                        return(error.ToJson());
                    }
                }
                else
                {
                    DebugHandler.TraceMessage("Could not add download with filesize: " + download.filesize + " due to insufficient space required: " + (UtilityMethods.GetFreeSpace(IrcSettings.fullfilepath) / 1024 / 1024).ToString(), DebugSource.TASK, DebugType.WARNING);

                    JsonError error = new JsonError()
                    {
                        type         = "unsufficient_space_error",
                        errormessage = "Could not add download with filesize: " + download.filesize + " due to insufficient space required: " + (UtilityMethods.GetFreeSpace(IrcSettings.fullfilepath) / 1024 / 1024).ToString(),
                        errortype    = "warning"
                    };
                    return(error.ToJson());
                }
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage("Could not add download with filesize: " + e.ToString(), DebugSource.TASK, DebugType.ERROR);

                JsonError error = new JsonError()
                {
                    type         = "add_download_error",
                    errormessage = "Could not add download to queue.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };
                return(error.ToJson());
            }
        }
Esempio n. 8
0
        public string AddDownloads(List <JsonDownloadInfo> download)
        {
            DebugHandler.TraceMessage("AddDownloads Called.", DebugSource.TASK, DebugType.ENTRY_EXIT);

            try
            {
                long totalSizeNeeded = 0;
                foreach (JsonDownloadInfo downloadinfo in download)
                {
                    DebugHandler.TraceMessage(downloadinfo.ToString(), DebugSource.TASK, DebugType.PARAMETERS);
                    if (downloadinfo.filesize.Contains("."))
                    {
                        downloadinfo.filesize = ((int)(double.Parse(downloadinfo.filesize, System.Globalization.CultureInfo.InvariantCulture) * 1024)).ToString();
                        totalSizeNeeded      += (int)(double.Parse(downloadinfo.filesize, System.Globalization.CultureInfo.InvariantCulture) * 1024);
                    }
                    else
                    {
                        totalSizeNeeded += int.Parse(downloadinfo.filesize);
                    }
                }

                if (UtilityMethods.GetFreeSpace(IrcSettings.fullfilepath) > (totalSizeNeeded * 1024 * 1024))
                {
                    DownloadQueue.AddRange(download);


                    DebugHandler.TraceMessage("Succesfully added " + download.Count + " downloads to download queue.", DebugSource.TASK, DebugType.INFO);

                    JsonSuccess succes = new JsonSuccess()
                    {
                        message = "Succesfully added " + download.Count + " downloads to download queue."
                    };

                    return(succes.ToJson());
                }
                else
                {
                    DebugHandler.TraceMessage("Could not add downloads with filesize: " + totalSizeNeeded + " due to insufficient space required: " + (UtilityMethods.GetFreeSpace(IrcSettings.fullfilepath) / 1024 / 1024).ToString(), DebugSource.TASK, DebugType.WARNING);


                    JsonError error = new JsonError()
                    {
                        type         = "unsufficient_space_error",
                        errormessage = "Could not add download with filesize: " + totalSizeNeeded + " due to insufficient space required: " + (UtilityMethods.GetFreeSpace(IrcSettings.fullfilepath) / 1024 / 1024).ToString(),
                        errortype    = "warning"
                    };
                    return(error.ToJson());
                }
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage("Could not add downloads: " + e.ToString(), DebugSource.TASK, DebugType.WARNING);
                JsonError error = new JsonError()
                {
                    type         = "add_download_error",
                    errormessage = "Could not add download to queue.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };
                return(error.ToJson());
            }
        }
Esempio n. 9
0
        public string RemoveDownload(string id = null, string filepath = null)
        {
            DebugHandler.TraceMessage("Remove Called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage("ID: " + id, DebugSource.TASK, DebugType.PARAMETERS);
            DebugHandler.TraceMessage("FILEPATH: " + filepath, DebugSource.TASK, DebugType.PARAMETERS);

            try
            {
                int index = 0;

                foreach (JsonDownloadInfo queuedDownload in DownloadQueue)
                {
                    if (id != null)
                    {
                        if (queuedDownload.id == id)
                        {
                            DebugHandler.TraceMessage("Removed download at index: " + index.ToString() + " using id: " + id, DebugSource.TASK, DebugType.INFO);
                            DownloadQueue.RemoveAt(index);
                            break;
                        }
                    }
                    else if (filepath != null)
                    {
                        if (Path.Combine(queuedDownload.fullfilepath, queuedDownload.filename) == filepath)
                        {
                            DebugHandler.TraceMessage("Removed download at index: " + index.ToString() + " using filepath: " + filepath, DebugSource.TASK, DebugType.INFO);
                            DownloadQueue.RemoveAt(index);
                            break;
                        }
                    }
                    else
                    {
                        DebugHandler.TraceMessage("Could not remove download from queue, neither id or filepath is defined.", DebugSource.TASK, DebugType.WARNING);
                        JsonError error = new JsonError()
                        {
                            type         = "remove_download_error",
                            errormessage = "Could not remove download from queue, neither id or filepath is defined.",
                            errortype    = "warning"
                        };
                        return(error.ToJson());
                    }
                    index++;
                }

                JsonSuccess succes = new JsonSuccess()
                {
                    message = "Succesfully removed download from download queue by download json."
                };

                return(succes.ToJson());
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage("Could not remove download from queue by download json: " + e.ToString(), DebugSource.TASK, DebugType.ERROR);

                JsonError error = new JsonError()
                {
                    type         = "remove_download_error",
                    errormessage = "Could not remove download from queue by download json.",
                    errortype    = "exception"
                };
                return(error.ToJson());
            }
        }
Esempio n. 10
0
        public string OpenFile(string filePath, string fileName = null)
        {
            DebugHandler.TraceMessage("OpenFile called", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage("FilePath: " + filePath, DebugSource.TASK, DebugType.PARAMETERS);

            string fullFilePath = filePath;

            if (fileName != null)
            {
                DebugHandler.TraceMessage("FileName: " + fileName, DebugSource.TASK, DebugType.PARAMETERS);

                fullFilePath = Path.Combine(filePath, fileName);

                DebugHandler.TraceMessage("Full Filepath: " + fullFilePath, DebugSource.TASK, DebugType.INFO);
            }

            try
            {
                for (int i = 0; i < 20; i++)
                {
                    if (File.Exists(fullFilePath))
                    {
#if __ANDROID__
                        Android.Net.Uri uri    = Android.Net.Uri.Parse(fullFilePath);
                        Intent          intent = new Intent(Intent.ActionView);
                        intent.SetDataAndType(uri, "video/*");
                        intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
                        Android.App.Application.Context.StartActivity(intent);
#else
                        if (UtilityMethods.CheckOperatingSystems() == UtilityMethods.OperatingSystems.OsX)
                        {
                            Process.Start("open", fullFilePath);
                        }
                        else
                        {
                            var p = new Process
                            {
                                StartInfo = new ProcessStartInfo(fullFilePath)
                                {
                                    UseShellExecute = true
                                }
                            };
                            p.Start();
                        }
#endif
                        JsonSuccess report = new JsonSuccess()
                        {
                            message = "Succesfully opened file with path: " + fullFilePath
                        };
                        return(report.ToJson());
                    }
                    Thread.Sleep(200);
                }

                JsonError jsonError = new JsonError
                {
                    type         = "open_file_failed",
                    errormessage = "Could not open file but didn't throw exception.",
                    errortype    = "warning",
                    exception    = "none"
                };

                DebugHandler.TraceMessage("Could not open file but didn't throw exception, file: " + fullFilePath, DebugSource.TASK, DebugType.WARNING);

                return(jsonError.ToJson());
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage("Could not open file. " + fullFilePath, DebugSource.TASK, DebugType.WARNING);
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);


                JsonError jsonError = new JsonError
                {
                    type         = "open_file_failed",
                    errormessage = "Could not open file.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };
                return(jsonError.ToJson());
            }
        }
Esempio n. 11
0
        public string DeleteFile(string filePath, string fileName = null)
        {
            DebugHandler.TraceMessage("DeleteFile called", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage("FilePath: " + filePath, DebugSource.TASK, DebugType.PARAMETERS);

            string fullFilePath = filePath;

            if (fileName != null)
            {
                DebugHandler.TraceMessage("FileName: " + fileName, DebugSource.TASK, DebugType.PARAMETERS);
                fullFilePath = Path.Combine(filePath, fileName);

                DebugHandler.TraceMessage("Full File Path: " + fullFilePath, DebugSource.TASK, DebugType.INFO);
            }

            try
            {
                if (File.Exists(fullFilePath))
                {
                    File.Delete(fullFilePath);

                    string[] filePaths = Directory.GetFiles(Path.GetDirectoryName(filePath));
                    if (filePaths.Length == 0)
                    {
                        Directory.Delete(filePath);

                        DebugHandler.TraceMessage("Succesfully deleted file: " + fullFilePath + " & empty directory: " + filePath, DebugSource.TASK, DebugType.INFO);

                        JsonSuccess report = new JsonSuccess()
                        {
                            message = "Succesfully deleted file with path: " + fullFilePath + " and directory: " + filePath
                        };

                        return(report.ToJson());
                    }
                    else
                    {
                        DebugHandler.TraceMessage("Succesfully deleted file: " + fullFilePath, DebugSource.TASK, DebugType.INFO);

                        JsonSuccess report = new JsonSuccess()
                        {
                            message = "Succesfully deleted file with path: " + fullFilePath
                        };

                        return(report.ToJson());
                    }
                }
                else
                {
                    DebugHandler.TraceMessage("File: " + fullFilePath + " does not exist, possibly already deleted.", DebugSource.TASK, DebugType.INFO);
                    JsonSuccess report = new JsonSuccess()
                    {
                        message = "File with filepath: " + fullFilePath + " already removed."
                    };

                    return(report.ToJson());
                }
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage("Failed to delete file: " + fullFilePath, DebugSource.TASK, DebugType.WARNING);
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);

                JsonError jsonError = new JsonError
                {
                    type         = "delete_file_failed",
                    errormessage = "Could not delete file.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };

                return(jsonError.ToJson());
            }
        }
 public ActionResult <JsonResponse> DeleteAuthenticatedSiteComponentMethod(ComponentReference Component)
 {
     if (Component.component_type == "p_box")
     {
         try{
             ParagraphBox paragraph_box = dbQuery.DeleteParagraphBox(Component.component_id);
             Site         parent_site   = dbQuery.QueryFeaturelessSiteById(paragraph_box.site_id);
             _dataLimiter.RemoveFromDataPlan(paragraph_box, parent_site.admin_id);
             JsonResponse r = new JsonSuccess("Paragraph box deleted sucessfully!");
             return(r);
         }catch {
             JsonFailure f = new JsonFailure($"Unable to find paragraph box id {Component.component_id}");
             return(StatusCode(400, f));
         }
     }
     else if (Component.component_type == "image")
     {
         try{
             Image image       = dbQuery.DeleteImage(Component.component_id);
             Site  parent_site = dbQuery.QueryFeaturelessSiteById(image.site_id);
             _dataLimiter.RemoveFromDataPlan(image, parent_site.admin_id);
             JsonResponse r = new JsonSuccess("Image deleted sucessfully!");
             return(r);
         }catch {
             JsonFailure f = new JsonFailure($"Unable to find image id {Component.component_id}");
             return(StatusCode(400, f));
         }
     }
     else if (Component.component_type == "portrait")
     {
         try{
             Portrait portrait    = dbQuery.DeletePortrait(Component.component_id);
             Site     parent_site = dbQuery.QueryFeaturelessSiteById(portrait.site_id);
             _dataLimiter.RemoveFromDataPlan(portrait, parent_site.admin_id);
             JsonResponse r = new JsonSuccess("Portrait component deleted sucessfully!");
             return(r);
         }catch {
             JsonFailure f = new JsonFailure($"Unable to find portrait id {Component.component_id}");
             return(StatusCode(400, f));
         }
     }
     else if (Component.component_type == "2c_box")
     {
         try{
             TwoColumnBox two_column_box = dbQuery.DeleteTwoColumnBox(Component.component_id);
             Site         parent_site    = dbQuery.QueryFeaturelessSiteById(two_column_box.site_id);
             _dataLimiter.RemoveFromDataPlan(two_column_box, parent_site.admin_id);
             JsonResponse r = new JsonSuccess("Two Column Box component deleted sucessfully!");
             return(r);
         }catch {
             JsonFailure f = new JsonFailure($"Unable to find two column box id {Component.component_id}");
             return(StatusCode(400, f));
         }
     }
     else if (Component.component_type == "link_box")
     {
         try{
             LinkBox link_box    = dbQuery.DeleteLinkBox(Component.component_id);
             Site    parent_site = dbQuery.QueryFeaturelessSiteById(link_box.site_id);
             _dataLimiter.RemoveFromDataPlan(link_box, parent_site.admin_id);
             JsonResponse r = new JsonSuccess("Link Box component deleted sucessfully!");
             return(r);
         }catch {
             JsonFailure f = new JsonFailure($"Unable to find link box id {Component.component_id}");
             return(StatusCode(400, f));
         }
     }
     else
     {
         JsonFailure f = new JsonFailure("Type mismatch. Type does not match any known components.");
         return(StatusCode(400, f));
     }
 }