public void HandleMessage(MessageBase message, SendHostMessageDelegate sendHostMessage)
        {
            var msg = message as ClientAutoCreatePlaylists;

            if (msg == null)
            {
                throw new ArgumentException("Message is not the right type");
            }
            if (msg.MaxPerNamePlaylist.HasValue && msg.MaxPerNamePlaylist.Value < 5)
            {
                throw new Exception("You're insane. less than 5 per playlist?");
            }
            var op  = new AutoCreatePlaylistsOp(msg.SortMode, msg.MaxPerNamePlaylist ?? 50, msg.RemoveEmptyPlaylists);
            var qae = _getQae();

            op.OpFinished += (s, e) =>
            {
                if (e.Status == OpStatus.Complete)
                {
                    _getConfig().Config = qae.GetCurrentConfig();
                }
            };
            qae.OpManager.QueueOp(op);
        }
Ejemplo n.º 2
0
        public void HandleRequest(HttpListenerContext context)
        {
            var req  = context.Request;
            var resp = context.Response;

            if (!Monitor.TryEnter(PLAYLIST_LOCK))
            {
                resp.BadRequest("Another playlist request is in progress.");
            }
            try
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(req.Url.Query))
                    {
                        resp.BadRequest("Expected sortorder");
                        return;
                    }
                    PlaylistSortMode?sortOrder = null;
                    int? maxNumPerNamePlaylist = null;
                    bool?removeEmptyPlaylists  = null;
                    foreach (string kvp in req.Url.Query.TrimStart('?').Split("&"))
                    {
                        var split = kvp.Split('=');
                        if (split.Count() < 1)
                        {
                            continue;
                        }
                        if (split[0].ToLower() == "sortorder")
                        {
                            PlaylistSortMode ord;
                            if (!Enum.TryParse(split[1], out ord))
                            {
                                resp.BadRequest("sortorder is invalid");
                                return;
                            }
                            sortOrder = ord;
                        }
                        else if (split[0].ToLower() == "maxnumpernameplaylist")
                        {
                            int maxNum;
                            if (!Int32.TryParse(split[1], out maxNum) || maxNum < 5)
                            {
                                resp.BadRequest("maxnumpernameplaylist is invalid or must be at least 5");
                                return;
                            }
                            maxNumPerNamePlaylist = maxNum;
                        }
                        else if (split[0].ToLower() == "removeemptyplaylists")
                        {
                            int maxNum;
                            if (split[1] == "1" || split[1].ToLower() == "true")
                            {
                                removeEmptyPlaylists = true;
                            }
                        }
                    }
                    if (!sortOrder.HasValue)
                    {
                        resp.BadRequest("Expected sortorder");
                        return;
                    }

                    var op = new AutoCreatePlaylistsOp(sortOrder.Value, maxNumPerNamePlaylist ?? 50, removeEmptyPlaylists ?? false);

                    _getQae().OpManager.QueueOp(op);
                    op.FinishedEvent.WaitOne();
                    if (op.Status == OpStatus.Failed)
                    {
                        throw new Exception("Op failed", op.Exception);
                    }
                    _getConfig().Config = _getQae().GetCurrentConfig();
                    resp.Ok();
                }
                catch (Exception ex)
                {
                    Log.LogErr("Exception handling auto create playlist!", ex);
                    resp.StatusCode = 500;
                }
            }
            finally
            {
                Monitor.Exit(PLAYLIST_LOCK);
            }
        }