Ejemplo n.º 1
0
        private static Dictionary <String, PluginBase> InitializePlugin(List <PluginBase> pluginsToLoad)
        {
            Dictionary <String, PluginBase> plugins = new Dictionary <String, PluginBase>();

            pluginsToLoad.ForEach(x =>
            {
                // initialize
                Boolean initialized = x.Initialize();

                if (!initialized)
                {
                    LoggerBundle.Warn($"Plugin '{x.Name}' cannot be initialized. This plugin will be deactivated.");
                    return;
                }

                LoggerBundle.Debug($"Plugin '{x.Name}' initialized successfully. Validating...");

                // validate
                String pcName = x.Name.ToLower().Trim();
                if (plugins.ContainsKey(pcName))
                {
                    LoggerBundle.Warn($"Plugin '{x.Name}' does not pass validation because a plugin with the same name has already been registered. This plugin will be deactivated.");
                }

                LoggerBundle.Debug($"Plugin '{x.Name}' passed validation");

                // add to plugin registry
                plugins.Add(pcName, x);
                LoggerBundle.Inform($"Plugin '{x.Name}' activated");
            });
            return(plugins);
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
0
        public static T Request <T>(String path) where T : class
        {
            LoggerBundle.Debug($"Requested configuration '{path}'");

            if (!File.Exists(path))
            {
                LoggerBundle.Trace($"File '{path}' not found. Trying to create it...");
                FileInterface.Save(Activator.CreateInstance <T>(), path);
                LoggerBundle.Trace($"Successfully created file '{path}'");

                LoggerBundle.Inform(
                    $"Changes to the newly created file '{path}' will take effect after restarting the executable. Adjust default value as needed and restart the application.");
            }

            LoggerBundle.Trace($"Trying to read file '{path}'...");
            (T result, Boolean success) = FileInterface.Read <T>(path);
            if (!success)
            {
                LoggerBundle.Warn(new ProcessAbortedException());
                return(null);
            }

            LoggerBundle.Debug($"Successfully read configuration file '{path}'");
            return(result);
        }
Ejemplo n.º 4
0
        protected override void Process(String[] args)
        {
            OnProcessStarting();
            TriggerActions(args.ToList());

            List <String> paths = args.Distinct().Select(x => x.Trim()).ToList();

            if (paths.Count.Equals(0))
            {
                LoggerBundle.Fatal(new ArgumentException("no argument given"));
                Environment.Exit(1);
            }

            foreach (String path in paths)
            {
                LoggerBundle.Inform($"Processing path '{path}'...");
                if (!Directory.Exists(path))
                {
                    LoggerBundle.Warn($"Path '{path}' not found. Skipping.");
                    continue;
                }

                LoggerBundle.Debug("Preloading data...");
                List <String> tracks;
                Stopwatch     sw = new Stopwatch();
                sw.Start();
                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    tracks = dataContext.SetTracks.AsNoTracking().Select(x => x.Path).ToList();
                }
                sw.Stop();
                LoggerBundle.Debug($"Getting data finished in {sw.ElapsedMilliseconds}ms");

                List <String>       buffer = new List <String>();
                DataSource <String> ds     = new PathDataSource(path, _config.Extensions);

                LoggerBundle.Inform($"Start to crawl path '{path}'...");
                foreach (String file in ds.Get())
                {
                    buffer.Add(file);

                    Int32 bufferCount = buffer.Count;
                    if (bufferCount < _config.BufferSize)
                    {
                        if (bufferCount % (_config.BufferSize < 1337 ? _config.BufferSize : 1337) == 0)
                        {
                            LoggerBundle.Trace($"Adding files to buffer [{bufferCount}/{_config.BufferSize}] ...");
                        }
                        continue;
                    }

                    ProcessBuffer(ref buffer, ref tracks);
                }

                ProcessBuffer(ref buffer, ref tracks);
            }
        }
Ejemplo n.º 5
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());
 }
Ejemplo n.º 6
0
        public IActionResult Get(Int32?id)
        {
            try
            {
                LoggerBundle.Trace("Registered GET request on FilesController.Get");

                if (!IsAuthorized(out IActionResult result))
                {
                    LoggerBundle.Trace("Request not authorized");
                    return(result);
                }

                // validate
                if (id == null)
                {
                    LoggerBundle.Trace("Validation failed: id is null");
                    return(StatusCode(StatusCodes.Status400BadRequest));
                }

                // get track
                Track track;
                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    track = dataContext.SetTracks.FirstOrDefault(x => x.UniqueId.Equals(id));
                }

                // validate
                if (null == track || !System.IO.File.Exists(track.Path))
                {
                    LoggerBundle.Trace($"Track not found for given id '{id}'");
                    return(StatusCode(StatusCodes.Status404NotFound));
                }

                FileSystemInfo file = new FileInfo(track.Path);
                if (!file.Exists)
                {
                    LoggerBundle.Warn($"File '{track.Path}' not found for given track. Cleanup your database.");
                    return(StatusCode(StatusCodes.Status404NotFound));
                }

                // get mime type
                if (!new FileExtensionContentTypeProvider().TryGetContentType(file.Name, out String contentType))
                {
                    // default fallback
                    contentType = $"audio/{file.Extension.Substring(1)}";
                }

                return(PhysicalFile(file.FullName, contentType));
            }
            catch (Exception ex)
            {
                return(HandleException(ex));
            }
        }
Ejemplo n.º 7
0
        public void TriggerAction(String key)
        {
            if (key == null)
            {
                LoggerBundle.Warn(new ArgumentNullException(nameof(key)));
                return;
            }

            if (!_actions.ContainsKey(key))
            {
                LoggerBundle.Debug(new KeyNotFoundException($"No action with name '{key}' found"));
                return;
            }

            _actions[key].Invoke();
        }
Ejemplo n.º 8
0
        public void RegisterAction(String key, Action action)
        {
            if (action == null)
            {
                LoggerBundle.Warn(new ArgumentNullException(nameof(action)));
                return;
            }

            if (key == null)
            {
                LoggerBundle.Warn(new ArgumentNullException(nameof(key)));
                return;
            }

            _actions[key] = action;
        }
Ejemplo n.º 9
0
        private IActionResult ProcessPayload(JwtPayload payload)
        {
            // generate token
            String token = JwtGenerator.GetToken(payload, Config.Authorization.Secret, Config.Authorization.ExpirationShift);

            if (String.IsNullOrWhiteSpace(token))
            {
                LoggerBundle.Warn("JWT token generation failed: empty token");
                return(StatusCode((Int32)HttpStatusCode.InternalServerError));
            }

            return(Ok(new Dictionary <String, String>
            {
                {
                    "token", token
                }
                ,
                {
                    "expires", payload.Exp.ToString(CultureInfo.InvariantCulture)
                }
            }));
        }
Ejemplo n.º 10
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);
            }
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
        protected Boolean IsAuthorized(out IActionResult statusCode, Func <User, Boolean> customUserAuthorization = null)
        {
            try
            {
                JwtPayload payload = _contextAuthenticatorPipe.Process(HttpContext);

                using (DataContext context = DataContextFactory.GetInstance())
                {
                    User user = context.SetUsers.Include(x => x.Invites)
                                .ThenInclude(x => x.CreateUser)
                                .Include(x => x.Invite)
                                .ThenInclude(x => x.RegisteredUser)
                                .Include(x => x.Playlists)
                                .ThenInclude(x => x.PlaylistEntries)
                                .Include(x => x.Playlists)
                                .ThenInclude(x => x.CreateUser)
                                .Include(x => x.Playlists)
                                .ThenInclude(x => x.PlaylistPermissions)
                                .Include(x => x.PlaylistEntries)
                                .ThenInclude(x => x.CreateUser)
                                .Include(x => x.PlaylistEntries)
                                .ThenInclude(x => x.Playlist)
                                .Include(x => x.PlaylistEntries)
                                .ThenInclude(x => x.Track)
                                .Include(x => x.PlaylistPermissions)
                                .ThenInclude(x => x.Playlist)
                                .Include(x => x.PlaylistPermissions)
                                .ThenInclude(x => x.User)
                                .FirstOrDefault(x => x.UniqueId.Equals(payload.ClientId) && x.Username.ToLower().Equals(payload.Name));

                    if (null == user)
                    {
                        LoggerBundle.Warn($"Got valid payload for user which is not in database: '{payload.Name}'");
                        statusCode = StatusCode((Int32)HttpStatusCode.Unauthorized);
                        return(false);
                    }

                    if (null != customUserAuthorization)
                    {
                        Boolean customAuthorization = customUserAuthorization.Invoke(user);
                        if (!customAuthorization)
                        {
                            LoggerBundle.Warn($"Got valid payload for user '{payload.Name}' but custom authorization failed");
                            statusCode = StatusCode((Int32)HttpStatusCode.Unauthorized);
                            return(false);
                        }
                    }

                    AuthorizedUser = user;
                }

                statusCode        = null;
                AuthorizedPayload = payload;
                return(true);
            }
            catch (Exception ex)
            {
                LoggerBundle.Trace(ex);
                statusCode = StatusCode((Int32)HttpStatusCode.Unauthorized);
                return(false);
            }
        }