Esempio n. 1
0
        public void HandleRequest(HttpListenerContext context)
        {
            var req  = context.Request;
            var resp = context.Response;

            try
            {
                if (string.IsNullOrWhiteSpace(req.Url.Query))
                {
                    resp.BadRequest("Expected modid");
                    return;
                }
                string modid = null;
                foreach (string kvp in req.Url.Query.TrimStart('?').Split("&"))
                {
                    var split = kvp.Split('=');
                    if (split.Count() < 1)
                    {
                        continue;
                    }
                    if (split[0].ToLower() == "modid")
                    {
                        modid = Java.Net.URLDecoder.Decode(split[1]);
                        break;
                    }
                }
                if (string.IsNullOrEmpty(modid))
                {
                    resp.BadRequest("Expected modid");
                    return;
                }
                var mod = _getConfig().Config.Mods.Where(x => x.ID == modid).FirstOrDefault();
                if (mod == null)
                {
                    Log.LogErr($"mod id {mod.id} doesn't seem to be loaded, so can't find image file for it");
                    resp.NotFound();
                    return;
                }
                if (string.IsNullOrEmpty(mod.CoverImageFilename) || !_config.RootFileProvider.FileExists(Constants.MODS_FOLDER_NAME.CombineFwdSlash(modid).CombineFwdSlash(mod.CoverImageFilename)))
                {
                    Log.LogErr($"mod ID {mod.ID} doesn't seem to have a cover image, or it doesn't exist.");
                    resp.NotFound();
                    return;
                }
                var mime = MimeMap.GetMimeType(mod.CoverImageFilename);
                var data = _config.RootFileProvider.Read(Constants.MODS_FOLDER_NAME.CombineFwdSlash(modid).CombineFwdSlash(mod.CoverImageFilename));
                resp.StatusCode  = 200;
                resp.ContentType = mime;
                resp.OutputStream.Write(data, 0, data.Length);
            }
            catch (Exception ex)
            {
                Log.LogErr("Exception handling get mod cover!", ex);
                resp.StatusCode = 500;
            }
        }
Esempio n. 2
0
        public void HandleRequest(HttpListenerContext context)
        {
            var req  = context.Request;
            var resp = context.Response;

            try
            {
                if (string.IsNullOrWhiteSpace(req.Url.Query))
                {
                    resp.BadRequest("Expected filename");
                    return;
                }
                string filename = null;
                foreach (string kvp in req.Url.Query.TrimStart('?').Split("&"))
                {
                    var split = kvp.Split('=');
                    if (split.Count() < 1)
                    {
                        continue;
                    }
                    if (split[0].ToLower() == "filename")
                    {
                        filename = Java.Net.URLDecoder.Decode(split[1]);
                        break;
                    }
                }
                if (string.IsNullOrEmpty(filename))
                {
                    resp.BadRequest("Expected filename");
                    return;
                }
                //yes, I am aware this can leak relative paths, I just care less about fixing it in this scenario than I do about typing this comment
                var fullfile = _config.PlaylistsPath.CombineFwdSlash(filename);
                if (!_config.RootFileProvider.FileExists(fullfile))
                {
                    resp.NotFound();
                    return;
                }

                byte[] fileBytes = _config.SongFileProvider.Read(fullfile);
                string mimeType  = MimeMap.GetMimeType(fullfile.GetFilenameFwdSlash());
                resp.StatusCode  = 200;
                resp.ContentType = mimeType;
                resp.AppendHeader("Cache-Control", "max-age=86400, public");
                resp.OutputStream.Write(fileBytes, 0, fileBytes.Length);
                return;
            }
            catch (Exception ex)
            {
                Log.LogErr("Exception handling get image!", ex);
                resp.StatusCode = 500;
            }
        }
Esempio n. 3
0
        public void HandleRequest(HttpListenerContext context)
        {
            var req  = context.Request;
            var resp = context.Response;

            try
            {
                if (string.IsNullOrWhiteSpace(req.Url.Query))
                {
                    resp.BadRequest("Expected songid");
                    return;
                }
                string songid = null;
                foreach (string kvp in req.Url.Query.TrimStart('?').Split("&"))
                {
                    var split = kvp.Split('=');
                    if (split.Count() < 1)
                    {
                        continue;
                    }
                    if (split[0].ToLower() == "songid")
                    {
                        songid = Java.Net.URLDecoder.Decode(split[1]);
                        break;
                    }
                }
                if (string.IsNullOrEmpty(songid))
                {
                    resp.BadRequest("Expected songid");
                    return;
                }
                var song = _getConfig().Config.Playlists.SelectMany(x => x.SongList).FirstOrDefault(x => x.SongID == songid);
                if (song == null)
                {
                    resp.NotFound();
                    return;
                }
                string originalFile = _config.SongsPath.CombineFwdSlash(GetCoverImageFilename(song.SongID));
                if (originalFile != null)
                {
                    try
                    {
                        byte[] fileBytes = _config.SongFileProvider.Read(originalFile);
                        string mimeType  = MimeMap.GetMimeType(originalFile.GetFilenameFwdSlash());
                        resp.StatusCode  = 200;
                        resp.ContentType = mimeType;
                        resp.AppendHeader("Cache-Control", "max-age=86400, public");
                        resp.OutputStream.Write(fileBytes, 0, fileBytes.Length);
                        return;
                    }
                    catch (Exception ex)
                    {
                        Log.LogErr($"Exception trying to load original song cover for song ID {song.SongID} from disk, will fall back to assets", ex);
                    }
                }

                try
                {
                    lock (_songCounterLock)
                    {
                        _songRequestCounter++;
                        if (_songRequestCounter > MAX_SONG_COVER_REQS)
                        {
                            resp.StatusCode = 429;
                            return;
                        }
                    }
                    lock (_songCoverLock)
                    {
                        var imgBytes = song.TryGetCoverPngBytes();
                        if (imgBytes == null)
                        {
                            resp.Error();
                            return;
                        }
                        resp.StatusCode  = 200;
                        resp.ContentType = MimeMap.GetMimeType("test.png");
                        resp.AppendHeader("Cache-Control", "max-age=86400, public");
                        using (MemoryStream ms = new MemoryStream(imgBytes))
                        {
                            ms.CopyTo(resp.OutputStream);
                        }
                    }
                }
                finally
                {
                    lock (_songCounterLock)
                    {
                        _songRequestCounter--;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.LogErr("Exception handling get song cover!", ex);
                resp.StatusCode = 500;
            }
        }
Esempio n. 4
0
        public static string CreateMail_MultiPart(string To, string Subject, string Content, string password)
        {
            string User_name = Operations.GlobalVarriable.User[0];

            if (User_name.IndexOf(" (") != -1)
            {
                User_name = User_name.Substring(0, Operations.GlobalVarriable.User[0].IndexOf(" ("));
            }
            User_name = Convert.ToBase64String(Encoding.UTF8.GetBytes(User_name));
            if (!String.IsNullOrEmpty(password))
            {
                switch (Operations.GlobalVarriable.KindOfCrypt)
                {
                case 0:
                {
                    Content = Operations.AES.Encrypt(Content, password);
                    break;
                }

                case 1:
                {
                    Content = Operations.DES.Encrypt(Content, password);
                    break;
                }

                case 2:
                {
                    Content = Operations.TwoFish.Encrypt(Content, password);
                    break;
                }

                case 3:
                {
                    Content = Operations.BlowFish.Encrypt(Content, password);
                    break;
                }

                default:
                    Content = Operations.AES.Encrypt(Content, password);
                    break;
                }
            }
            string Edit =
                "Content-Type: multipart/mixed; boundary=\"foo_bar_baz\"\r\n" +
                "MIME-Version: 1.0\r\n" +
                "From: " + "=?UTF-8?B?" + User_name + "?=" + " <" + Operations.GlobalVarriable.User[1] + ">" + "\r\n" +
                "To: " + To + "\r\n" +
                "Subject: " + "=?UTF-8?B?" + Convert.ToBase64String(Encoding.UTF8.GetBytes(Subject)) + "?=" + "\r\n\r\n" +

                "--foo_bar_baz\r\n" +
                "Content-Type: text/html; charset=\"UTF-8\"\r\n" +
                "MIME-Version: 1.0\r\n" +
                "Content-Transfer-Encoding: 7bit\r\n\r\n" +

                Content + "\r\n\r\n";

            foreach (var item in Send.Att)
            {
                if (item == null || item == String.Empty || !File.Exists(item))
                {
                    continue;
                }
                Edit += "--foo_bar_baz\r\n" +
                        "Content-Type: " + MimeMap.GetMimeType(Path.GetExtension(item)) + "\r\n" +
                        "MIME-Version: 1.0\r\n" +
                        "Content-Transfer-Encoding: base64\r\n" +
                        "Content-Disposition: attachment; filename=\"" + Path.GetFileName(item) + "\"\r\n\r\n" +
                        (String.IsNullOrEmpty(password) ? Convert.ToBase64String(File.ReadAllBytes(item)) :
                         (Operations.GlobalVarriable.KindOfCrypt == 0 ? Operations.AES.EncryptFile(item, password) :
                          (Operations.GlobalVarriable.KindOfCrypt == 1 ? Operations.DES.EncryptFile(item, password) :
                           (Operations.GlobalVarriable.KindOfCrypt == 2 ? Operations.TwoFish.EncryptFile(item, password) :
                            Operations.BlowFish.EncryptFile(item, password))))) + "\r\n\r\n";
            }
            Edit += "--foo_bar_baz--";
            Send.Att.Clear();
            return(Edit);
        }
Esempio n. 5
0
        public void InvalidMimeTypeTest()
        {
            var mimeType = MimeMap.GetMimeType(".!@#");

            Assert.AreEqual(null, mimeType);
        }
Esempio n. 6
0
        public void GetMimeTypeTest()
        {
            var mimeType = MimeMap.GetMimeType(".TxT");

            Assert.AreEqual("text/plain", mimeType);
        }
Esempio n. 7
0
 /// <summary>
 /// Creates a FilePathResult object by using the path to the file. Gets the content type from MimeMap.GetMimeType based on the files extension.
 /// </summary>
 /// <param name="fileName"></param>
 /// <returns></returns>
 public FileResult File(string fileName)
 {
     return(base.File(fileName, MimeMap.GetMimeType(Path.GetExtension(fileName))));
 }
Esempio n. 8
0
        public void HandleRequest(HttpListenerContext context)
        {
            var req  = context.Request;
            var resp = context.Response;

            try
            {
                if (string.IsNullOrWhiteSpace(req.Url.Query))
                {
                    resp.BadRequest("Expected playlistid");
                    return;
                }
                string playlistid = null;
                foreach (string kvp in req.Url.Query.TrimStart('?').Split("&"))
                {
                    var split = kvp.Split('=');
                    if (split.Count() < 1)
                    {
                        continue;
                    }
                    if (split[0].ToLower() == "playlistid")
                    {
                        playlistid = Java.Net.URLDecoder.Decode(split[1]);
                        break;
                    }
                }
                if (string.IsNullOrEmpty(playlistid))
                {
                    resp.BadRequest("Expected playlistid");
                    return;
                }
                var playlist = _getConfig().Config.Playlists.FirstOrDefault(x => x.PlaylistID == playlistid);
                if (playlist == null)
                {
                    resp.NotFound();
                    return;
                }
                var  pngName        = _config.PlaylistsPath.CombineFwdSlash(playlistid + ".png");
                bool coverWasLoaded = playlist.IsCoverLoaded;
                //if the playlist cover is already loaded, there's a good chance it's from an updated asset which hasn't written the PNG to disk yet, so
                //  that should skip the filesystem.
                if (!coverWasLoaded)
                {
                    try
                    {
                        if (_config.RootFileProvider.FileExists(pngName))
                        {
                            var pngData = _config.RootFileProvider.Read(pngName);
                            resp.StatusCode  = 200;
                            resp.ContentType = MimeMap.GetMimeType("test.png");
                            resp.AppendHeader("Cache-Control", "no-cache");
                            resp.OutputStream.Write(pngData, 0, pngData.Length);
                            Log.LogMsg($"Returned playlist cover art from file {pngName} for id {playlistid}");
                            return;
                        }
                        else
                        {
                            Log.LogMsg($"Playlist image {pngName} didn't exist, falling back to assets files.");
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.LogErr($"Exception loading png for playlist {playlistid}, falling back to assets files.", ex);
                    }
                }
                lock (_playlistCoverLock)
                {
                    var imgBytes = playlist.TryGetCoverPngBytes();
                    if (imgBytes == null)
                    {
                        resp.Error();
                        return;
                    }
                    try
                    {
                        if (!coverWasLoaded && !_config.RootFileProvider.FileExists(pngName))
                        {
                            Log.LogMsg($"Trying to create cover art file at {pngName} for playlist ID {playlistid} so it's available next time");
                            _config.RootFileProvider.MkDir(_config.PlaylistsPath, true);
                            _config.RootFileProvider.Write(pngName, imgBytes, true);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.LogErr($"Exception creating cover art file at {pngName} for playlist ID {playlistid}", ex);
                    }
                    resp.StatusCode  = 200;
                    resp.ContentType = MimeMap.GetMimeType("test.png");
                    resp.AppendHeader("Cache-Control", "max-age=86400, public");
                    using (MemoryStream ms = new MemoryStream(imgBytes))
                    {
                        ms.CopyTo(resp.OutputStream);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.LogErr("Exception handling get playlist cover!", ex);
                resp.StatusCode = 500;
            }
        }