Exemple #1
0
        private void HandleErrOutput(DataReceivedEventArgs arguments, Track track)
        {
            LoggerBundle.Trace($"Processing error response of track '{track}'...");
            try
            {
                String output = arguments?.Data?.Trim();
                if (String.IsNullOrEmpty(output))
                {
                    return;
                }

                LoggerBundle.Warn(new CalculationException(output));
                track.FingerprintError           = output;
                track.LastFingerprintCalculation = DateTime.Now;

                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    LoggerBundle.Trace($"Saving track '{track}'...");
                    dataContext.SetTracks.Attach(track);
                    dataContext.Entry(track).State = EntityState.Modified;
                    dataContext.SaveChanges();
                    LoggerBundle.Debug($"Successfully updated track '{track}'...");
                }
            }
            catch (Exception ex)
            {
                LoggerBundle.Error(ex);
            }
            finally
            {
                _buffer.Remove(track);
            }
        }
Exemple #2
0
 private static void Main(String[] args)
 {
     // mainly for debugging
     LoggerBundle.Trace("TEST");
     LoggerBundle.Debug("TEST");
     LoggerBundle.Inform("TEST");
     LoggerBundle.Warn(new Exception());
     LoggerBundle.Error(new Exception());
     LoggerBundle.Fatal(new Exception());
 }
Exemple #3
0
        private void HandleStdOutput(DataReceivedEventArgs arguments, Track track)
        {
            LoggerBundle.Trace($"Processing response of track '{track}'...");
            try
            {
                String output = arguments?.Data?.Trim();
                if (String.IsNullOrEmpty(output))
                {
                    return;
                }

                LoggerBundle.Debug(Logger.DefaultLogFlags & ~LogFlags.SuffixNewLine
                                   , $"Trying to serialize computation output of file '{track}'...");
                JsonFingerprint jfp = JsonConvert.DeserializeObject <JsonFingerprint>(output);
                LoggerBundle.Debug(Logger.DefaultLogFlags & ~LogFlags.PrefixLoggerType & ~LogFlags.PrefixTimeStamp, "Ok.");

                track.LastFingerprintCalculation = DateTime.Now;
                track.FingerprintHash            = _hasher.Compute(jfp.Fingerprint);
                track.FingerprintError           = null;

                LoggerBundle.Trace($"Fingerprint hash: {track.FingerprintHash} for fingerprint {jfp.Fingerprint}");

                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    LoggerBundle.Trace($"Checking for duplicates for file '{track}'...");
                    if (dataContext.SetTracks.AsNoTracking().Any(x => x.FingerprintHash.Equals(track.FingerprintHash)))
                    {
                        LoggerBundle.Debug($"File with same fingerprint already in database. Path '{track}' will be skipped");
                        track.FingerprintError = "duplicate";
                    }
                    else
                    {
                        LoggerBundle.Trace($"No duplicate found for file '{track}'");
                        track.Duration    = jfp.Duration;
                        track.Fingerprint = jfp.Fingerprint;
                        LoggerBundle.Trace($"New meta data duration '{track.Duration}' and fingerprint '{jfp.Fingerprint}'");
                    }

                    LoggerBundle.Trace($"Saving file '{track.Path}'...");
                    dataContext.SetTracks.Attach(track);
                    dataContext.Entry(track).State = EntityState.Modified;
                    dataContext.SaveChanges();
                    LoggerBundle.Debug($"Successfully saved file '{track}'...");
                }
            }
            catch (Exception ex)
            {
                LoggerBundle.Error(ex);
            }
            finally
            {
                _buffer.Remove(track);
            }
        }
Exemple #4
0
        public void Work(String[] args)
        {
            if (!IsInitialized)
            {
                LoggerBundle.Error($"Plugin '{Name}'", new NotInitializedException());
                return;
            }

            OnProcessStarting();
            Process(args);
            OnProcessStopping();
        }
Exemple #5
0
        public IActionResult Login([FromBody] AuthenticationModel values)
        {
            try
            {
                LoggerBundle.Trace("Registered POST request on LoginsController.Login");

                //validate data
                String passwordBase64 = values?.Password;
                if (String.IsNullOrWhiteSpace(passwordBase64) || String.IsNullOrWhiteSpace(values.Username))
                {
                    LoggerBundle.Trace("Validation failed: empty username or password");
                    return(StatusCode((Int32)HttpStatusCode.Unauthorized));
                }

                // hash password
                Byte[] bPassword    = Convert.FromBase64String(passwordBase64);
                String password     = Encoding.UTF8.GetString(bPassword);
                String passwordHash = new Sha512HashPipe().Process(password);

                // normalize username
                values.Username = values.Username.Trim();

                // check database for given username
                User user;
                using (DataContext dc = DataContextFactory.GetInstance())
                {
                    user = dc.SetUsers.AsNoTracking().FirstOrDefault(x => x.Username.Equals(values.Username));
                }

                if (null == user)
                {
                    LoggerBundle.Trace($"No user found for given username '{values.Username}'");
                    return(StatusCode((Int32)HttpStatusCode.Unauthorized));
                }

                if (!user.Password.Equals(passwordHash))
                {
                    LoggerBundle.Trace($"Login attempt for user '{user.Username}' failed");
                    return(StatusCode((Int32)HttpStatusCode.Unauthorized));
                }

                // prepare token generation
                JwtPayload payload = UserJwtPayloadPipe.Process(user);
                return(ProcessPayload(payload));
            }
            catch (Exception ex)
            {
                LoggerBundle.Error(ex);
                return(StatusCode((Int32)HttpStatusCode.Unauthorized));
            }
        }
Exemple #6
0
        private static void Main(String[] args)
        {
            try
            {
                new Program();
            }
            catch (Exception ex)
            {
                LoggerBundle.Error(ex);
            }

#if DEBUG
            Console.ReadKey();
#endif
        }
Exemple #7
0
        public static Boolean Save <T>(T obj, String path) where T : class
        {
            if (null == obj)
            {
                LoggerBundle.Error(new ArgumentNullException(nameof(obj)));
                return(false);
            }

            if (null == path)
            {
                LoggerBundle.Error(new ArgumentNullException(nameof(path)));
                return(false);
            }

            if (File.Exists(path))
            {
                LoggerBundle.Error(new PathOccupiedException());
                return(false);
            }

            try
            {
                LoggerBundle.Trace(Logger.DefaultLogFlags & ~LogFlags.SuffixNewLine
                                   , $"Trying to serialize obj for file '{path}'...");
                String text = JsonConvert.SerializeObject(obj);
                LoggerBundle.Trace(Logger.DefaultLogFlags & ~LogFlags.PrefixLoggerType & ~LogFlags.PrefixTimeStamp, "Ok.");
                LoggerBundle.Trace($"Serialized object for file '{path}' is '{text}'");
                String pathRoot = Path.GetDirectoryName(path);
                if (!Directory.Exists(pathRoot))
                {
                    LoggerBundle.Trace(Logger.DefaultLogFlags & ~LogFlags.SuffixNewLine
                                       , $"Directory '{pathRoot}' does not exist. Trying to create it...");
                    Directory.CreateDirectory(pathRoot);
                    LoggerBundle.Trace(Logger.DefaultLogFlags & ~LogFlags.PrefixLoggerType & ~LogFlags.PrefixTimeStamp, "Ok.");
                }
                LoggerBundle.Trace(Logger.DefaultLogFlags & ~LogFlags.SuffixNewLine, $"Writing object to file '{path}'...");
                File.WriteAllText(path, text);
                LoggerBundle.Trace(Logger.DefaultLogFlags & ~LogFlags.PrefixLoggerType & ~LogFlags.PrefixTimeStamp, "Ok.");
                return(true);
            }
            catch (Exception ex)
            {
                LoggerBundle.Error(ex);
                return(false);
            }
        }
Exemple #8
0
        public Boolean Initialize()
        {
            try
            {
                LoggerBundle.Trace($"Initializing plugin '{Name}'...");
                OnInitialize();
                LoggerBundle.Trace($"Successfully initialized plugin '{Name}'");

                RegisterDefaultActions();
                IsInitialized = true;
            }
            catch (Exception ex)
            {
                LoggerBundle.Error(ex);
            }

            return(IsInitialized);
        }
Exemple #9
0
        private static (Boolean success, ProgramConfig config) ProcessProgramArguments(String[] args)
        {
            ProgramConfig config;

            try
            {
                LoggerBundle.Trace("Trying to parse to following arguments:");
                args.ToList().ForEach(x => LoggerBundle.Trace($"+ '{x}'"));
                config = CliParser.Parse <ProgramConfig>(args);
            }
            catch (Exception ex)
            {
                LoggerBundle.Error(ex);
                return(false, null);
            }

            return(true, config);
        }
Exemple #10
0
        private void CleanInvisible()
        {
            List <Track> data;

            do
            {
                LoggerBundle.Inform("Removing invisible data");
                LoggerBundle.Debug("Getting data...");
                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    data = dataContext.SetTracks.Where(x => null != x.LastAcoustIdApiCall).Where(x => 1 > x.AcoustIdResults.Count).OrderBy(x => x.UniqueId).Take(_config.BufferSize).ToList();

                    LoggerBundle.Inform($"Batch containing: {data.Count} entries");

                    foreach (Track track in data)
                    {
                        try
                        {
                            LoggerBundle.Debug($"Processing track '{track}'...");

                            if (File.Exists(track.Path))
                            {
                                File.Delete(track.Path);
                            }

                            dataContext.SetTracks.Remove(track);
                            dataContext.SaveChanges();

                            LoggerBundle.Trace("Processing done");
                        }
                        catch (Exception ex)
                        {
                            LoggerBundle.Error(ex);
                        }
                    }
                }
            }while (data.Count > 0);
        }
Exemple #11
0
        private Program(String[] args)
        {
            LoggerBundle.Inform("Initializing...");

            (Boolean success, ProgramConfig config) = ProcessProgramArguments(args);
            if (!success)
            {
                LoggerBundle.Fatal("Program arguments could not be parsed correctly");
                Environment.Exit(1);
            }

            try
            {
                CreateGlobalDirectories();

                List <PluginBase> pluginsToLoad         = LoadPlugins();
                Dictionary <String, PluginBase> plugins = InitializePlugin(pluginsToLoad);

                String pluginName = config.PluginName.ToLower();
                if (!plugins.ContainsKey(pluginName))
                {
                    LoggerBundle.Warn($"No active plugin with name '{pluginName}' found");
                    LoggerBundle.Inform("The following plugin names were registered on startup: ");
                    plugins.Keys.ToList().ForEach(x => LoggerBundle.Inform($"+ {x}"));
                    return;
                }

                LoggerBundle.Inform("Executing plugin...");
                plugins[pluginName].Work(config.Args.ToArray());
                LoggerBundle.Inform("Execution finished.");
            }
            catch (Exception ex)
            {
                LoggerBundle.Error(ex);
            }
        }
Exemple #12
0
        protected override void Process(String[] args)
        {
            base.OnProcessStarting();
            TriggerActions(args.ToList());

            List <Track> data;

            do
            {
                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    LoggerBundle.Debug("Loading batch...");

                    data = _includeFailed
                        ? dataContext.SetTracks
                           .Where(x => null != x.LastFingerprintCalculation &&
                                  null == x.FingerprintError &&
                                  null == x.LastAcoustIdApiCall ||
                                  x.LastAcoustIdApiCall.HasValue && null != x.AcoustIdApiError)
                           .Take(_config.BufferSize)
                           .ToList()
                        : dataContext.SetTracks
                           .Where(x => null != x.LastFingerprintCalculation &&
                                  null == x.FingerprintError &&
                                  null == x.LastAcoustIdApiCall)
                           .Take(_config.BufferSize)
                           .ToList();

                    LoggerBundle.Inform($"Batch containing {data.Count} entries");

                    foreach (Track track in data)
                    {
                        LoggerBundle.Debug($"Posting metadata of track '{track}'...");

                        track.LastAcoustIdApiCall = DateTime.Now;

                        Object response = _apiHandler.Post(track.Duration ?? 0d, track.Fingerprint);
                        LoggerBundle.Trace($"Response: {response}");

                        switch (response)
                        {
                        case JsonErrorAcoustId jea:
                        {
                            LoggerBundle.Warn(new AcoustIdApiException($"Error {jea.Error.Code}: {jea.Error.Message}"));
                            track.AcoustIdApiError     = jea.Error.Message;
                            track.AcoustIdApiErrorCode = jea.Error.Code;
                            break;
                        }

                        case JsonAcoustIdRequest air:
                        {
                            HandleResponse(dataContext, track, air);
                            break;
                        }

                        default:
                        {
                            LoggerBundle.Trace(Logger.DefaultLogFlags & ~LogFlags.SuffixNewLine
                                               , "Trying to serialize unknown response object...");
                            String serializedResponse = "<unknown>";
                            try
                            {
                                serializedResponse = JsonConvert.SerializeObject(response);
                            }
                            catch (Exception ex)
                            {
                                LoggerBundle.Error(ex);
                            }
                            LoggerBundle.Trace(Logger.DefaultLogFlags
                                               & ~LogFlags.PrefixTimeStamp
                                               & ~LogFlags.PrefixLoggerType
                                               , "Ok.");
                            LoggerBundle.Warn(new AcoustIdApiException($"Unknown response: {serializedResponse}"));
                            track.AcoustIdApiError = serializedResponse;
                            break;
                        }
                        }

                        dataContext.SaveChanges();
                    }
                }
            }while (data.Count > 0);
        }
        protected override void Process(String[] args)
        {
            OnProcessStarting();
            TriggerActions(args.ToList());

            List <MusicBrainzRecord> data;

            do
            {
                LoggerBundle.Debug("Getting data...");
                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    data = dataContext.SetMusicBrainzRecords.Where(x => null == x.LastMusicBrainzApiCall)
                           .Include(x => x.MusicBrainzAliasMusicBrainzRecords)
                           .ThenInclude(x => x.MusicBrainzAlias)
                           .Include(x => x.MusicBrainzArtistCreditMusicBrainzRecords)
                           .ThenInclude(x => x.MusicBrainzArtistCredit)
                           .Include(x => x.MusicBrainzReleaseMusicBrainzRecords)
                           .ThenInclude(x => x.MusicBrainzRelease)
                           .Include(x => x.MusicBrainzTagMusicBrainzRecords)
                           .ThenInclude(x => x.MusicBrainzTag)
                           .OrderBy(x => x.UniqueId)
                           .Take(_config.BatchSize)
                           .ToList();

                    LoggerBundle.Inform($"Batch containing: {data.Count} entries");

                    foreach (MusicBrainzRecord mbr in data)
                    {
                        try
                        {
                            LoggerBundle.Debug($"Processing record '{mbr}'...");

                            DateTime requestTime = DateTime.Now;
                            Object   o           = _api.Get(mbr.MusicbrainzId);

                            Stopwatch sw = new Stopwatch();
                            sw.Start();

                            switch (o)
                            {
                            case JsonMusicBrainzRequest req:
                                HandleResponse(mbr, req, dataContext);
                                break;

                            case JsonErrorMusicBrainz err:
                                mbr.MusicBrainzApiCallError = err.Error?.Trim() ?? "<unknown>";
                                LoggerBundle.Warn(new MusicBrainzApiException($"Error: {mbr.MusicBrainzApiCallError}"));
                                break;
                            }

                            mbr.LastMusicBrainzApiCall = requestTime;
                            dataContext.SaveChanges();

                            sw.Stop();
                            LoggerBundle.Debug($"Processing done in {sw.ElapsedMilliseconds}ms");
                        }
                        catch (Exception ex)
                        {
                            LoggerBundle.Error(ex);
                        }
                    }
                }
            }while (data.Count > 0);
        }
Exemple #14
0
        private void AddUser()
        {
            LoggerBundle.Debug("Starting process to add new user...");
            try
            {
                // read username
                LoggerBundle.Inform(Logger.DefaultLogFlags & ~LogFlags.SuffixNewLine, "Enter a username: "******"";

                if (String.IsNullOrWhiteSpace(username))
                {
                    LoggerBundle.Fatal(new ArgumentException("Username cannot be empty"));
                    Environment.Exit(1);
                }

                // check existance
                LoggerBundle.Debug("Checking if user already exists...");
                Boolean exists;
                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    exists = dataContext.SetUsers.Any(x => x.Username.ToLower().Equals(username.ToLower()));
                }
                if (exists)
                {
                    LoggerBundle.Fatal(new ArgumentException("Username already exists"));
                    Environment.Exit(1);
                }

                LoggerBundle.Trace("User not found database. Allowed to proceed forward");

                // get password
                LoggerBundle.Inform(Logger.DefaultLogFlags & ~LogFlags.SuffixNewLine, "Enter a password: "******"Confirm password: "******"Passwords do not match"));
                    Environment.Exit(1);
                }

                // hash password
                Sha512HashPipe hashPipe = new Sha512HashPipe();
                String         hashedPw = hashPipe.Process(pw1);

                // save model
                User user = new User
                {
                    Username   = username
                    , Password = hashedPw
                };
                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    dataContext.SetUsers.Add(user);
                    dataContext.SaveChanges();
                }
                LoggerBundle.Inform(
                    $"Successfully created user '{user.Username}' created with unique identifier '{user.UniqueId}'");
            }
            catch (Exception ex)
            {
                LoggerBundle.Error(ex);
            }
        }
 protected IActionResult HandleException(Exception ex)
 {
     LoggerBundle.Error(ex);
     return(StatusCode((Int32)HttpStatusCode.InternalServerError));
 }