private void EpisodeImageContent(HttpListenerContext context) { Regex epidPattern = new Regex(@"^\?epid=([0-9]+)$"); Match match = epidPattern.Match(context.Request.Url.Query); int epid; if (!int.TryParse(match.Groups[1].Value, out epid)) { // Request doesn't contain a valid episode id this.ErrorPage404(context); return; } Model.Episode episode; try { episode = new Model.Episode(epid); } catch (DataNotFoundException) { // Episode ID does not exist this.ErrorPage404(context); return; } CompressedImage image = episode.Image; if (image == null) { // Specified episode does not have an image this.ErrorPage404(context); return; } context.Response.ContentType = "image/png"; // Find the appropriate codec for saving the image as a png System.Drawing.Imaging.ImageCodecInfo pngCodec = null; foreach (System.Drawing.Imaging.ImageCodecInfo codec in System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()) { if (codec.MimeType == context.Response.ContentType) { pngCodec = codec; break; } } try { image.Image.Save(context.Response.OutputStream, pngCodec, null); } catch (HttpListenerException) { // Don't worry about dropped connections etc. } }
private void ChapterImageContent(HttpListenerContext context) { Regex paramsPattern = new Regex(@"^\?epid=([0-9]+)&start=([0-9]+)$"); Match match = paramsPattern.Match(context.Request.Url.Query); int epid, start; if (!int.TryParse(match.Groups[1].Value, out epid)) { // Request doesn't contain a valid episode id this.ErrorPage404(context); return; } if (!int.TryParse(match.Groups[2].Value, out start)) { // Request doesn't contain a valid start time this.ErrorPage404(context); return; } var download = new Model.Download(epid); var chapter = new Model.Chapter(download, start); CompressedImage image = chapter.Image; if (image == null) { // Specified chapter does not have an image this.ErrorPage404(context); return; } context.Response.ContentType = "image/png"; // Find the appropriate codec for saving the image as a png System.Drawing.Imaging.ImageCodecInfo pngCodec = null; foreach (System.Drawing.Imaging.ImageCodecInfo codec in System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()) { if (codec.MimeType == context.Response.ContentType) { pngCodec = codec; break; } } try { image.Image.Save(context.Response.OutputStream, pngCodec, null); } catch (HttpListenerException) { // Don't worry about dropped connections etc. } }
protected internal static int?StoreImage(CompressedImage image) { if (image == null) { return(null); } byte[] imageAsBytes = image.GetBytes(); lock (DbUpdateLock) { using (SQLiteCommand command = new SQLiteCommand("select imgid from images where image=@image", FetchDbConn())) { command.Parameters.Add(new SQLiteParameter("@image", imageAsBytes)); using (SQLiteMonDataReader reader = new SQLiteMonDataReader(command.ExecuteReader())) { if (reader.Read()) { return(reader.GetInt32(reader.GetOrdinal("imgid"))); } } } using (SQLiteCommand command = new SQLiteCommand("insert into images (image) values (@image)", FetchDbConn())) { command.Parameters.Add(new SQLiteParameter("@image", imageAsBytes)); command.ExecuteNonQuery(); } using (SQLiteCommand command = new SQLiteCommand("select last_insert_rowid()", FetchDbConn())) { return((int)(long)command.ExecuteScalar()); } } }