public async Task SetLittleWeebSettings(JObject jsonLittleWeebSettings)
        {
            DebugHandler.TraceMessage("SetLittleWeebSettings Called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(jsonLittleWeebSettings.ToString(), DebugSource.TASK, DebugType.PARAMETERS);

            try
            {
                LittleWeebSettings = SettingsHandler.GetLittleWeebSettings();

                LittleWeebSettings.RandomUsernameLength = jsonLittleWeebSettings.Value <int>("randomusernamelength");
                LittleWeebSettings.DebugLevel           = jsonLittleWeebSettings.Value <JArray>("debuglevel").ToObject <List <int> >();
                LittleWeebSettings.DebugType            = jsonLittleWeebSettings.Value <JArray>("debugtype").ToObject <List <int> >();
                LittleWeebSettings.MaxDebugLogSize      = jsonLittleWeebSettings.Value <int>("maxdebuglogsize");
                SetAllLittleWeebSettings(LittleWeebSettings);


                SettingsHandler.WriteLittleWeebSettings(LittleWeebSettings);

                await GetCurrentLittleWeebSettings();
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);
                JsonError error = new JsonError
                {
                    type         = "set_littleweeb_settings_error",
                    errortype    = "Exception",
                    errormessage = "Failed to set littleweeb settings.",
                    exception    = e.ToString()
                };

                await WebSocketHandler.SendMessage(error.ToJson());
            }
        }
Beispiel #2
0
        private async void OnIrcClientConnectionStatusEvent(object sender, IrcClientConnectionStatusArgs args)
        {
            DebugHandler.TraceMessage("OnIrcClientConnectionStatusEvent called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(args.ToString(), DebugSource.TASK, DebugType.PARAMETERS);


            IsIrcConnected = args.Connected;

            try
            {
                JsonIrcInfo update = new JsonIrcInfo()
                {
                    connected    = args.Connected,
                    channel      = args.CurrentIrcSettings.Channels,
                    server       = args.CurrentIrcSettings.ServerAddress,
                    user         = args.CurrentIrcSettings.UserName,
                    fullfilepath = args.CurrentIrcSettings.fullfilepath
                };

                await WebSocketHandler.SendMessage(update.ToJson());
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.ERROR);
                JsonError error = new JsonError()
                {
                    type         = "irc_status_error",
                    errormessage = "Error on sending irc status update to client.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };
                await WebSocketHandler.SendMessage(error.ToJson());
            }
        }
Beispiel #3
0
        public async Task DeleteDirectory(JObject directoryJson)
        {
            DebugHandler.TraceMessage("DeleteDirectory called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(directoryJson.ToString(), DebugSource.TASK, DebugType.PARAMETERS);
            try
            {
                string filePath = directoryJson.Value <string>("path");
                if (filePath.Contains("//"))
                {
                    filePath.Replace("//", "/");
                }

                if (filePath.Contains("\\\\"))
                {
                    filePath.Replace("\\\\", "\\");
                }
                string result = DirectoryHandler.DeleteDirectory(filePath);
                await WebSocketHandler.SendMessage(result);
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);

                JsonError error = new JsonError()
                {
                    type         = "create_directory_error",
                    errormessage = "Could not create directory.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };

                await WebSocketHandler.SendMessage(error.ToJson());
            }
        }
        public async Task Setfullfilepath(JObject fullfilepathJson)
        {
            DebugHandler.TraceMessage("Setfullfilepath Called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(fullfilepathJson.ToString(), DebugSource.TASK, DebugType.PARAMETERS);
            try
            {
                string path = fullfilepathJson.Value <string>("path");
                DirectoryHandler.CreateDirectory(path, "");
                IrcSettings.fullfilepath = path;
                SetAllIrcSettings(IrcSettings);
                SettingsHandler.WriteIrcSettings(IrcSettings);
                await GetCurrentIrcSettings();
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);

                JsonError jsonError = new JsonError
                {
                    type         = "set_download_directory_error",
                    errortype    = "Exception",
                    errormessage = "Failed to set custom download directory.",
                    exception    = e.ToString()
                };

                await WebSocketHandler.SendMessage(jsonError.ToJson());
            }
        }
        public async Task AbortDownload(JObject downloadJson)
        {
            DebugHandler.TraceMessage("AbortDownload called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(downloadJson.ToString(), DebugSource.TASK, DebugType.PARAMETERS);
            string id       = downloadJson.Value <string>("id");
            string filePath = downloadJson.Value <string>("path");

            if (id != null)
            {
                string result = await DownloadHandler.AbortDownload(id);

                await WebSocketHandler.SendMessage(result);
            }
            else if (filePath != null)
            {
                string result = await DownloadHandler.AbortDownload(null, filePath);

                await WebSocketHandler.SendMessage(result);
            }
            else
            {
                DebugHandler.TraceMessage("Neither id or file path have been defined!", DebugSource.TASK, DebugType.WARNING);
                JsonError error = new JsonError()
                {
                    type         = "parse_download_to_abort_error",
                    errormessage = "Neither id or file path have been defined!",
                    errortype    = "warning",
                    exception    = "none"
                };
                await WebSocketHandler.SendMessage(error.ToJson());
            }
        }
        public async Task SetIrcSettings(JObject jsonIrcSettings)
        {
            DebugHandler.TraceMessage("SetIrcSettings Called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(jsonIrcSettings.ToString(), DebugSource.TASK, DebugType.PARAMETERS);

            try
            {
                IrcSettings = SettingsHandler.GetIrcSettings();

                IrcSettings.ServerAddress = jsonIrcSettings.Value <string>("address");
                IrcSettings.Channels      = jsonIrcSettings.Value <string>("channels");
                IrcSettings.UserName      = jsonIrcSettings.Value <string>("username");
                IrcSettings.fullfilepath  = jsonIrcSettings.Value <string>("fullfilepath");
                IrcSettings.Port          = jsonIrcSettings.Value <int>("port");
                IrcSettings.Secure        = jsonIrcSettings.Value <bool>("secure");

                SetAllIrcSettings(IrcSettings);
                SettingsHandler.WriteIrcSettings(IrcSettings);
                await GetCurrentIrcSettings();
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);

                JsonError jsonError = new JsonError
                {
                    type         = "set_irc_settings_error",
                    errortype    = "Exception",
                    errormessage = "Failed to set irc settings.",
                    exception    = e.ToString()
                };

                await WebSocketHandler.SendMessage(jsonError.ToJson());
            }
        }
Beispiel #7
0
        public string DeleteDirectory(string path)
        {
            DebugHandler.TraceMessage("DeleteDirectory Called", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage("Directory Path: " + path, DebugSource.TASK, DebugType.PARAMETERS);
            try
            {
                DirectoryInfo info          = new DirectoryInfo(path);
                int           amountOfFiles = info.GetFiles().Length;
                if (amountOfFiles == 0)
                {
                    if (Directory.Exists(path))
                    {
                        Directory.Delete(path);

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

                        return(report.ToJson());
                    }
                    else
                    {
                        JsonSuccess report = new JsonSuccess()
                        {
                            message = "Directory with path : " + path + " already removed."
                        };

                        return(report.ToJson());
                    }
                }
                else
                {
                    DebugHandler.TraceMessage("Could not delete directory: " + path + " because there are still files and/or other directories inside!", DebugSource.TASK, DebugType.WARNING);


                    JsonError jsonError = new JsonError
                    {
                        type         = "delete_directory_warning",
                        errortype    = "warning",
                        errormessage = "Could not delete directory: " + path + " because there are still files and/or other directories inside!"
                    };

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

                JsonError jsonError = new JsonError
                {
                    type         = "delete_directory_error",
                    errortype    = "exception",
                    errormessage = "Could not get drives, see log."
                };

                return(jsonError.ToJson());
            }
        }
        public void OnWebSocketEvent(WebSocketEventArgs args)
        {
            DebugHandler.TraceMessage("OnWebSocketEvent Called", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(args.ToString(), DebugSource.TASK, DebugType.PARAMETERS);

            try
            {
                try{
                    JObject query  = JObject.Parse(args.Message);
                    string  action = query.Value <string>("action");

                    if (action != null)
                    {
                        JObject extra = query.Value <JObject>("extra");

                        if (extra != null)
                        {
                            switch (action)
                            {
                            case "delete_file":
                                FileWebSocketService.DeleteFile(extra);
                                break;

                            case "open_file":
                                FileWebSocketService.OpenFile(extra);
                                break;
                            }
                        }
                    }
                }
                catch (JsonReaderException e)
                {
                    DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);

                    JsonError error = new JsonError()
                    {
                        type         = "command_error",
                        errormessage = "Error happend during parsing of command.",
                        errortype    = "exception",
                        exception    = e.ToString()
                    };
                    WebSocketHandler.SendMessage(error.ToJson());
                }
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.ERROR);
                JsonError error = new JsonError()
                {
                    type         = "command_error",
                    errormessage = "Error happend during execution of command.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };
                WebSocketHandler.SendMessage(error.ToJson());
            }
        }
        public async Task AddDownloads(JObject downloadJsonBatch)
        {
            DebugHandler.TraceMessage("AddDownloads called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(downloadJsonBatch.ToString(), DebugSource.TASK, DebugType.PARAMETERS);

            try
            {
                JArray listWithDownloads = downloadJsonBatch.Value <JArray>("batch");

                List <JsonDownloadInfo> batch = new List <JsonDownloadInfo>();

                foreach (JObject downloadJson in listWithDownloads)
                {
                    JsonDownloadInfo downloadInfo = new JsonDownloadInfo()
                    {
                        animeInfo = new JsonAnimeInfo()
                        {
                            anime_id             = downloadJson.Value <JObject>("animeInfo").Value <string>("animeid"),
                            anime_title          = downloadJson.Value <JObject>("animeInfo").Value <string>("title"),
                            anime_cover_original = downloadJson.Value <JObject>("animeInfo").Value <string>("cover_original"),
                            anime_cover_small    = downloadJson.Value <JObject>("animeInfo").Value <string>("cover_small")
                        },
                        id            = downloadJson.Value <string>("id"),
                        episodeNumber = downloadJson.Value <string>("episodeNumber"),
                        pack          = downloadJson.Value <string>("pack"),
                        bot           = downloadJson.Value <string>("bot"),
                        fullfilepath  = downloadJson.Value <string>("dir"),
                        filename      = downloadJson.Value <string>("filename"),
                        progress      = downloadJson.Value <string>("progress"),
                        speed         = downloadJson.Value <string>("speed"),
                        status        = downloadJson.Value <string>("status"),
                        filesize      = downloadJson.Value <string>("filesize")
                    };

                    LastDownloadedInfo = downloadInfo;
                    batch.Add(downloadInfo);
                }

                string result = DownloadHandler.AddDownloads(batch);

                await WebSocketHandler.SendMessage(result);
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);

                JsonError error = new JsonError()
                {
                    type         = "parse_downloads_to_add_error",
                    errormessage = "Could not parse json containing downloads to add information.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };

                await WebSocketHandler.SendMessage(error.ToJson());
            }
        }
        public async Task RemoveDownload(JObject downloadJson)
        {
            DebugHandler.TraceMessage("RemoveDownload called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(downloadJson.ToString(), DebugSource.TASK, DebugType.PARAMETERS);

            try
            {
                string id       = downloadJson.Value <string>("id");
                string filePath = downloadJson.Value <string>("path");
                string result   = "";
                if (id != null)
                {
                    result = DownloadHandler.RemoveDownload(id, null);
                    await WebSocketHandler.SendMessage(result);

                    string toRemove     = FileHistoryHandler.RemoveFileFromFileHistory(id, null);
                    string resultRemove = FileHandler.DeleteFile(toRemove);
                    await WebSocketHandler.SendMessage(resultRemove);
                }
                else if (filePath != null)
                {
                    result = DownloadHandler.RemoveDownload(null, filePath);
                    await WebSocketHandler.SendMessage(result);

                    string toRemove     = FileHistoryHandler.RemoveFileFromFileHistory(null, filePath);
                    string resultRemove = FileHandler.DeleteFile(toRemove);
                    await WebSocketHandler.SendMessage(resultRemove);
                }
                else
                {
                    JsonError error = new JsonError()
                    {
                        type         = "parse_download_to_remove_error",
                        errormessage = "Neither id or file path have been defined!",
                        errortype    = "warning",
                        exception    = "none"
                    };
                    await WebSocketHandler.SendMessage(error.ToJson());
                }
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);

                JsonError error = new JsonError()
                {
                    type         = "parse_download_to_remove_error",
                    errormessage = "Could not parse json containing download to remove information.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };


                await WebSocketHandler.SendMessage(error.ToJson());
            }
        }
        public async Task OpenFile(JObject fileInfoJson)
        {
            DebugHandler.TraceMessage("OpenFile called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(fileInfoJson.ToString(), DebugSource.TASK, DebugType.PARAMETERS);

            try
            {
                string filePath = fileInfoJson.Value <string>("path");

                if (filePath.Contains("//"))
                {
                    filePath.Replace("//", "/");
                }

                if (filePath.Contains("\\\\"))
                {
                    filePath.Replace("\\\\", "\\");
                }

                if (filePath != null)
                {
                    string result = FileHandler.OpenFile(filePath);
                    await WebSocketHandler.SendMessage(result);
                }
                else
                {
                    DebugHandler.TraceMessage("Request does not contain path parameter to open file.", DebugSource.TASK, DebugType.WARNING);
                    JsonError error = new JsonError()
                    {
                        type         = "parse_file_to_open_error",
                        errormessage = "Request does not contain path parameter to open file.",
                        errortype    = "warning",
                        exception    = "none"
                    };

                    await WebSocketHandler.SendMessage(error.ToJson());
                }
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);
                JsonError error = new JsonError()
                {
                    type         = "parse_file_to_open_error",
                    errormessage = "Could not parse json containing file to open information.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };

                await WebSocketHandler.SendMessage(error.ToJson());
            }
        }
Beispiel #12
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());
            }
        }
Beispiel #13
0
        public async Task Disconnect()
        {
            DebugHandler.TraceMessage("Disconnect called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            bool succes = false;

            try
            {
                succes = IrcClientHandler.StopConnection();
                JsonIrcInfo ircInfo = new JsonIrcInfo()
                {
                    connected    = !succes,
                    channel      = IrcSettings.Channels,
                    server       = IrcSettings.ServerAddress,
                    user         = IrcSettings.UserName,
                    fullfilepath = IrcSettings.fullfilepath
                };

                await WebSocketHandler.SendMessage(ircInfo.ToJson());
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);

                succes = IrcClientHandler.StopConnection();
                JsonIrcInfo ircInfo = new JsonIrcInfo()
                {
                    connected    = !succes,
                    channel      = IrcSettings.Channels,
                    server       = IrcSettings.ServerAddress,
                    user         = IrcSettings.UserName,
                    fullfilepath = IrcSettings.fullfilepath
                };

                await WebSocketHandler.SendMessage(ircInfo.ToJson());

                JsonError error = new JsonError()
                {
                    type         = "irc_disconnect_error",
                    errormessage = "Could not stop connection to irc server.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };

                await WebSocketHandler.SendMessage(error.ToJson());
            }
        }
        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());
            }
        }
Beispiel #15
0
        public string CreateDirectory(string path, string name)
        {
            DebugHandler.TraceMessage("CreateDirectory Called", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage("Directory Path: " + path + ", Directory name: " + name, DebugSource.TASK, DebugType.PARAMETERS);


            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);

                    return(GetDirectories(path));
                }
                else
                {
                    DebugHandler.TraceMessage("Could not create directory: Directory already exists!", DebugSource.TASK, DebugType.WARNING);

                    JsonError jsonError = new JsonError
                    {
                        type         = "directory_already_exists",
                        errortype    = "warning",
                        errormessage = "Directory already exist."
                    };

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

                JsonError jsonError = new JsonError
                {
                    type         = "create_directory_error",
                    errortype    = "exception",
                    errormessage = "Could not  create directory, see log."
                };

                return(jsonError.ToJson());
            }
        }
Beispiel #16
0
        public string GetDirectories(string path)
        {
            DebugHandler.TraceMessage("GetDirectories Called", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage("Directory Path: " + path, DebugSource.TASK, DebugType.PARAMETERS);


            try
            {
                string[] dirs = Directory.GetDirectories(path);

                JsonDirectories      tosendover          = new JsonDirectories();
                List <JsonDirectory> directorieswithpath = new List <JsonDirectory>();
                foreach (string directory in dirs)
                {
                    JsonDirectory directorywithpath = new JsonDirectory
                    {
                        dirname = directory.Replace(Path.GetDirectoryName(directory) + Path.DirectorySeparatorChar, ""),
                        path    = directory
                    };
                    tosendover.directories.Add(directorywithpath);
                }
                return(tosendover.ToJson());
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage("Failed getting directories: " + e.ToString(), DebugSource.TASK, DebugType.WARNING);

                JsonError jsonError = new JsonError
                {
                    type         = "get_drives_error",
                    errortype    = "exception",
                    errormessage = "Could not get drives, see log.",
                    exception    = e.ToString()
                };

                return(jsonError.ToJson());
            }
        }
Beispiel #17
0
        private async void OnIrcClientMessageEvent(object sender, IrcClientMessageEventArgs args)
        {
            DebugHandler.TraceMessage("OnIrcClientMessageEvent called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(args.ToString(), DebugSource.TASK, DebugType.PARAMETERS);

            try
            {
                if (SendMessageToWebSocketClient)
                {
                    JsonIrcChatMessage messageToSend = new JsonIrcChatMessage()
                    {
                        channel = args.Channel,
                        user    = args.User,
                        message = args.Message
                    };

                    await WebSocketHandler.SendMessage(messageToSend.ToJson());
                }
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.ERROR);
                if (SendMessageToWebSocketClient)
                {
                    JsonError error = new JsonError()
                    {
                        type         = "irc_status_error",
                        errormessage = "Error on sending irc message to client.",
                        errortype    = "exception",
                        exception    = e.ToString()
                    };


                    await WebSocketHandler.SendMessage(error.ToJson());
                }
            }
        }
        public async Task DeleteFile(JObject fileInfoJson)
        {
            DebugHandler.TraceMessage("DeleteFile called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(fileInfoJson.ToString(), DebugSource.TASK, DebugType.PARAMETERS);

            try
            {
                string filePath = fileInfoJson.Value <string>("path");

                if (filePath.Contains("//"))
                {
                    filePath.Replace("//", "/");
                }

                if (filePath.Contains("\\\\"))
                {
                    filePath.Replace("\\\\", "\\");
                }

                DownloadHandler.RemoveDownload(filePath);
                FileHistoryHandler.RemoveFileFromFileHistory(filePath);
                string result = FileHandler.DeleteFile(filePath);
                await WebSocketHandler.SendMessage(result);
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);
                JsonError error = new JsonError()
                {
                    type         = "parse_file_to_delete_error",
                    errormessage = "Could not parse json containing file to delete information.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };
                await WebSocketHandler.SendMessage(error.ToJson());
            }
        }
Beispiel #19
0
        public async Task GetCurrentlyAiring()
        {
            DebugHandler.TraceMessage("GetCurrentlyAiring Called.", DebugSource.TASK, DebugType.ENTRY_EXIT);

            try
            {
                JsonCurrentlyAiring currentlyAiring = await AnimeProfileHandler.GetCurrentlyAiring();

                await WebSocketHandler.SendMessage(currentlyAiring.ToJson());
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.ERROR);
                JsonError error = new JsonError()
                {
                    type         = "get_currently_airing_error",
                    errormessage = "Could not get currently airing.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };

                await WebSocketHandler.SendMessage(error.ToJson());
            }
        }
Beispiel #20
0
        public async Task SendMessage(JObject ircMessageJson)
        {
            DebugHandler.TraceMessage("SendMessage called.", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(ircMessageJson.ToString(), DebugSource.TASK, DebugType.PARAMETERS);

            try
            {
                string message = ircMessageJson.Value <string>("message");
                IrcClientHandler.SendMessage(message);
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.ERROR);
                JsonError error = new JsonError()
                {
                    type         = "irc_connect_error",
                    errormessage = "Could not send message to irc server.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };

                await WebSocketHandler.SendMessage(error.ToJson());
            }
        }
Beispiel #21
0
        public async Task Connect(JObject ircJson = null)
        {
            DebugHandler.TraceMessage("Connect called", DebugSource.TASK, DebugType.ENTRY_EXIT);
            bool succes = false;

            try
            {
                if (ircJson == null)
                {
                    string username = IrcSettings.UserName;

                    if (username == "")
                    {
                        username = UtilityMethods.GenerateUsername(LittleWeebSettings.RandomUsernameLength);
                    }
                    IrcSettings.UserName = username;
                }
                else
                {
                    DebugHandler.TraceMessage(ircJson.ToString(), DebugSource.TASK, DebugType.INFO);

                    string username = ircJson.Value <string>("username");

                    if (username == "")
                    {
                        username = UtilityMethods.GenerateUsername(LittleWeebSettings.RandomUsernameLength);
                    }


                    IrcSettings.ServerAddress = ircJson.Value <string>("address");
                    IrcSettings.Channels      = ircJson.Value <string>("channels");
                    IrcSettings.UserName      = username;
                }

                succes = IrcClientHandler.StartConnection(IrcSettings);

                if (succes)
                {
                    JsonIrcInfo ircInfo = new JsonIrcInfo()
                    {
                        connected    = succes,
                        channel      = IrcSettings.Channels,
                        server       = IrcSettings.ServerAddress,
                        user         = IrcSettings.UserName,
                        fullfilepath = IrcSettings.fullfilepath
                    };

                    await WebSocketHandler.SendMessage(ircInfo.ToJson());
                }
                else
                {
                    JsonIrcInfo ircInfo = new JsonIrcInfo()
                    {
                        connected    = succes,
                        channel      = IrcSettings.Channels,
                        server       = IrcSettings.ServerAddress,
                        user         = IrcSettings.UserName,
                        fullfilepath = IrcSettings.fullfilepath
                    };

                    await WebSocketHandler.SendMessage(ircInfo.ToJson());
                }

                DebugHandler.TraceMessage("Started irc connection using the following settings: " + IrcSettings.ToString(), DebugSource.TASK, DebugType.INFO);

                SettingsHandler.WriteIrcSettings(IrcSettings);
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);

                JsonIrcInfo ircInfo = new JsonIrcInfo()
                {
                    connected    = succes,
                    channel      = IrcSettings.Channels,
                    server       = IrcSettings.ServerAddress,
                    user         = IrcSettings.UserName,
                    fullfilepath = IrcSettings.fullfilepath
                };

                await WebSocketHandler.SendMessage(ircInfo.ToJson());

                JsonError error = new JsonError()
                {
                    type         = "irc_connect_error",
                    errormessage = "Could not start connection to irc server.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };

                await WebSocketHandler.SendMessage(error.ToJson());
            }

            IsIrcConnected = succes;
        }
Beispiel #22
0
        public void OnWebSocketEvent(WebSocketEventArgs args)
        {
            DebugHandler.TraceMessage("OnWebSocketEvent called", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(args.ToString(), DebugSource.TASK, DebugType.PARAMETERS);


            try
            {
                try
                {
                    JObject query  = JObject.Parse(args.Message);
                    string  action = query.Value <string>("action");

                    if (action != null)
                    {
                        JObject extra = query.Value <JObject>("extra");

                        if (extra != null)
                        {
                            switch (action)
                            {
                            case "get_files_for_anime":
                                InfoApiWebSocketService.GetFilesForAnime(extra);
                                break;

                            case "get_anime_profile":
                                InfoApiWebSocketService.GetAnimeProfile(extra);
                                break;
                            }
                        }
                        else
                        {
                            switch (action)
                            {
                            case "get_currently_airing":
                                InfoApiWebSocketService.GetCurrentlyAiring();
                                break;
                            }
                        }
                    }
                }
                catch (JsonReaderException e)
                {
                    DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);

                    JsonError error = new JsonError()
                    {
                        type         = "command_error",
                        errormessage = "Error happend during parsing of command.",
                        errortype    = "exception",
                        exception    = e.ToString()
                    };
                    WebSocketHandler.SendMessage(error.ToJson());
                }
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.ERROR);

                JsonError error = new JsonError()
                {
                    type         = "command_error",
                    errormessage = "Error happend during execution of command.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };
                WebSocketHandler.SendMessage(error.ToJson());
            }
        }
        public void OnWebSocketEvent(WebSocketEventArgs args)
        {
            DebugHandler.TraceMessage("OnWebSocketEvent Called", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(args.ToString(), DebugSource.TASK, DebugType.PARAMETERS);


            try
            {
                try{
                    JObject query  = JObject.Parse(args.Message);
                    string  action = query.Value <string>("action");

                    if (action != null)
                    {
                        JObject extra = query.Value <JObject>("extra");

                        if (extra != null)
                        {
                            switch (action)
                            {
                            case "set_littleweeb_settings":
                                SettingsWebSocketService.SetLittleWeebSettings(extra);
                                break;

                            case "set_irc_settings":
                                SettingsWebSocketService.SetIrcSettings(extra);
                                break;

                            case "set_download_directory":
                                SettingsWebSocketService.Setfullfilepath(extra);
                                break;
                            }
                        }
                        else
                        {
                            switch (action)
                            {
                            case "get_littleweeb_settings":
                                SettingsWebSocketService.GetCurrentLittleWeebSettings();
                                break;

                            case "get_irc_settings":
                                SettingsWebSocketService.GetCurrentIrcSettings();
                                break;
                            }
                        }
                    }
                }
                catch (JsonReaderException e)
                {
                    DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);
                    JsonError error = new JsonError()
                    {
                        type         = "command_error",
                        errormessage = "Error happend during parsing of command.",
                        errortype    = "exception",
                        exception    = e.ToString()
                    };
                    WebSocketHandler.SendMessage(error.ToJson());
                }
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.ERROR);

                JsonError error = new JsonError()
                {
                    type         = "command_error",
                    errormessage = "Error happend during execution of command.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };
                WebSocketHandler.SendMessage(error.ToJson());
            }
        }
Beispiel #24
0
        public void OnWebSocketEvent(WebSocketEventArgs args)
        {
            DebugHandler.TraceMessage("OnWebSocketEvent Called", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(args.ToString(), DebugSource.TASK, DebugType.PARAMETERS);
            try
            {
                try{
                    JObject query  = JObject.Parse(args.Message);
                    string  action = query.Value <string>("action");

                    if (action != null)
                    {
                        JObject extra = query.Value <JObject>("extra");

                        if (extra != null)
                        {
                            switch (action)
                            {
                            case "connect_irc":
                                IrcWebSocketService.Connect(extra);
                                break;

                            case "sendmessage_irc":
                                IrcWebSocketService.SendMessage(extra);
                                break;
                            }
                        }
                        else
                        {
                            switch (action)
                            {
                            case "get_irc_data":
                                IrcWebSocketService.GetCurrentIrcSettings();
                                break;

                            case "connect_irc":
                                IrcWebSocketService.Connect();
                                break;

                            case "disconnect_irc":
                                IrcWebSocketService.Disconnect();
                                break;

                            case "enablechat_irc":
                                IrcWebSocketService.EnableSendMessage();
                                break;

                            case "disablechat_irc":
                                IrcWebSocketService.DisableSendMessage();
                                break;
                            }
                        }
                    }
                }
                catch (JsonReaderException e)
                {
                    DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);
                    JsonError error = new JsonError()
                    {
                        type         = "command_error",
                        errormessage = "Error happend during parsing of command.",
                        errortype    = "exception",
                        exception    = e.ToString()
                    };
                    WebSocketHandler.SendMessage(error.ToJson());
                }
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.ERROR);
                JsonError error = new JsonError()
                {
                    type         = "command_error",
                    errormessage = "Error happend during execution of command.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };
                WebSocketHandler.SendMessage(error.ToJson());
            }
        }
Beispiel #25
0
        public void OnWebSocketEvent(WebSocketEventArgs args)
        {
            DebugHandler.TraceMessage("OnWebSocketEvent Called", DebugSource.TASK, DebugType.ENTRY_EXIT);
            DebugHandler.TraceMessage(args.ToString(), DebugSource.TASK, DebugType.PARAMETERS);

            try
            {
                try
                {
                    JObject query  = JObject.Parse(args.Message);
                    string  action = query.Value <string>("action");

                    if (action != null)
                    {
                        JObject extra = query.Value <JObject>("extra");

                        if (extra != null)
                        {
                            switch (action)
                            {
                            case "add_download":
                                DownloadWebSocketService.AddDownload(extra);
                                break;

                            case "add_downloads":
                                DownloadWebSocketService.AddDownloads(extra);
                                break;

                            case "abort_download":
                                DownloadWebSocketService.AbortDownload(extra);
                                break;

                            case "remove_download":
                                DownloadWebSocketService.RemoveDownload(extra);
                                break;
                            }
                        }
                        else
                        {
                            switch (action)
                            {
                            case "get_downloads":
                                DownloadWebSocketService.GetCurrentFileHistory();
                                break;

                            case "get_free_space":
                                DirectoryWebSocketService.GetFreeSpace();
                                break;

                            case "open_download_directory":
                                DownloadWebSocketService.Openfullfilepath();
                                break;
                            }
                        }
                    }
                }
                catch (JsonReaderException e)
                {
                    DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING);

                    JsonError error = new JsonError()
                    {
                        type         = "command_error",
                        errormessage = "Error happend during parsing of command.",
                        errortype    = "exception",
                        exception    = e.ToString()
                    };
                    WebSocketHandler.SendMessage(error.ToJson());
                }
            }
            catch (Exception e)
            {
                DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.ERROR);
                JsonError error = new JsonError()
                {
                    type         = "command_error",
                    errormessage = "Error happend during execution of command.",
                    errortype    = "exception",
                    exception    = e.ToString()
                };
                WebSocketHandler.SendMessage(error.ToJson());
            }
        }
        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());
            }
        }
        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());
            }
        }
        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());
            }
        }
Beispiel #29
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());
            }
        }
Beispiel #30
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());
            }
        }