コード例 #1
0
        public static WPostFile GetFileByChanbName(string name, DbConnection con)
        {
            WPostFile pf = null;

            using (DbCommand dc = DatabaseEngine.GenerateDbCommand(con))
            {
                dc.CommandText = "SELECT ID, postID, md5, realname, size, extension, mimetype, dimension FROM files WHERE (chanbname = @chanbname)";
                dc.Parameters.Add(DatabaseEngine.MakeParameter("@chanbname", name, DbType.String));

                using (DbDataReader reader = dc.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        if (Convert.IsDBNull(reader[0]))
                        {
                            break;
                        }

                        pf = new WPostFile()
                        {
                            PostID     = Convert.ToInt32(ReadParam(reader[1])),
                            Hash       = Convert.ToString(ReadParam(reader[2])),
                            ChanbName  = name,
                            RealName   = Convert.ToString(ReadParam(reader[3])),
                            Size       = Convert.ToInt32(ReadParam(reader[4])),
                            Extension  = Convert.ToString(ReadParam(reader[5])),
                            MimeType   = Convert.ToString(ReadParam(reader[6])),
                            Dimensions = Convert.ToString(ReadParam(reader[7])),
                        };
                    }
                }
            }

            return(pf);
        }
コード例 #2
0
        private static string get_file_html_noscript(WPostFile file)
        {
            StringBuilder sb = new StringBuilder(TemplateProvider.NoScriptFile);

            sb.Replace("{file:link}", file.ImageWebPath);
            sb.Replace("{file:thumblink}", file.ImageThumbnailWebPath);
            sb.Replace("{file:name}", file.ChanbName);
            return(sb.ToString());
        }
コード例 #3
0
        private static string get_file_html(WPostFile file, bool isInRotator, bool isNext)
        {
            StringBuilder s;

            switch (file.Extension.ToLower())
            {
            case "jpg":
            case "jpeg":
            case "png":
            case "bmp":
            case "gif":
            case "apng":
            case "svg":
            case "pdf":
                s = get_imagefile_html(file);
                break;

            case "webm":
                s = get_videofile_html(file);
                break;

            case "ogg":
            case "mp3":
                s = get_audiofile_html(file);
                break;

            default:
                //check custom files
                return("");
            }

            if (isInRotator)
            {
                s.Replace("{html:filec}", "");

                if (isNext)
                {
                    s.Replace("{rotator:isnext}", "hide");
                }
                else
                {
                    s.Replace("{rotator:isnext}", "file");
                }
            }
            else
            {
                s.Replace("{html:filec}", "file");
                s.Replace("{rotator:isnext}", "");
            }

            return(s.ToString());
        }
コード例 #4
0
        public static void AddFileToDatabase(WPostFile WPF, DbConnection con)
        {
            using (DbCommand dc = DatabaseEngine.GenerateDbCommand(con))
            {
                dc.CommandText = "INSERT INTO files (postID, chanbname, realname, size, md5, extension, mimetype, dimension) " +
                                 " VALUES  (@postID, @chanbname, @realname, @size, @md5, @extension, @mimetype, @dimension)";

                dc.Parameters.Add(DatabaseEngine.MakeParameter("@postID", WPF.PostID, DbType.Int32));
                dc.Parameters.Add(DatabaseEngine.MakeParameter("@chanbname", WPF.ChanbName, DbType.String));
                dc.Parameters.Add(DatabaseEngine.MakeParameter("@realname", WPF.RealName, DbType.String));
                dc.Parameters.Add(DatabaseEngine.MakeParameter("@size", WPF.Size, DbType.Int64));
                dc.Parameters.Add(DatabaseEngine.MakeParameter("@md5", WPF.Hash, DbType.String));
                dc.Parameters.Add(DatabaseEngine.MakeParameter("@extension", WPF.Extension, DbType.String));
                dc.Parameters.Add(DatabaseEngine.MakeParameter("@mimetype", WPF.MimeType, DbType.String));
                dc.Parameters.Add(DatabaseEngine.MakeParameter("@dimension", WPF.Dimensions, DbType.String));

                dc.ExecuteNonQuery();
            }
        }
コード例 #5
0
        private static StringBuilder get_audiofile_html(WPostFile pf)
        {
            StringBuilder audio_temp = new StringBuilder(TemplateProvider.AudioFile);

            audio_temp.Replace("{file:id}", pf.PostID.ToString());

            audio_temp.Replace("{file:dl-link}", pf.ImageWebPath); // HACK

            audio_temp.Replace("{file:link}", pf.ImageWebPath);

            audio_temp.Replace("{file:name}", pf.RealName);
            audio_temp.Replace("{file:size}", format_size_string(pf.Size));
            audio_temp.Replace("{file:dimensions}", pf.Dimensions);

            audio_temp.Replace("{mime}", get_mime(pf.Extension));

            audio_temp.Replace("{lang:noaudiosupport}", Lang.noaudiosupport);

            return(audio_temp);
        }
コード例 #6
0
        private static StringBuilder get_imagefile_html(WPostFile pf)
        {
            StringBuilder image_template = new StringBuilder(TemplateProvider.ImageFile);

            image_template.Replace("{file:id}", pf.PostID.ToString());

            image_template.Replace("{file:dl-link}", pf.ImageWebPath); // HACK

            image_template.Replace("{file:link}", pf.ImageWebPath);
            image_template.Replace("{file:thumblink}", pf.ImageThumbnailWebPath);

            image_template.Replace("{file:name}", pf.RealName);
            image_template.Replace("{file:size}", format_size_string(pf.Size));
            image_template.Replace("{file:dimensions}", pf.Dimensions);

            image_template.Replace("{file:ext}", pf.Extension.ToUpper());

            image_template.Replace("{file:selinks}", "");

            return(image_template);
        }
コード例 #7
0
        public static void save_post_file(int postID, HttpPostedFile file, DbConnection con)
        {
            string file_extension = file.FileName.Split('.').Last().ToLower();

            if (Array.IndexOf(ApplicationSettings.DisabledFiles, file_extension) >= 0)
            {
                throw new Exception(string.Format("File type '{0}' is disabled", file_extension));
            }

            string file_hash = Common.Misc.MD5(file.InputStream);

            if (!ApplicationSettings.AllowDuplicatesFiles)
            {
                bool file_exist_indb = FileExistInDatabase(file_hash, -1, con);

                if (file_exist_indb)
                {
                    if (ApplicationSettings.SmartLinkDuplicateImages)
                    {
                        WPostFile wpf = GetFileByHash(file_hash, con);

                        wpf.RealName = file.FileName;
                        wpf.PostID   = postID;

                        AddFileToDatabase(wpf, con);
                        return;
                    }
                    else
                    {
                        throw new Exception(string.Format("Duplicate file '{0}' with the hash (md5) '{1}'", file.FileName, file_hash));
                    }
                }
            }  //no need to check for duplicate files if duplicate files are allowed.


            string chanb_name = string.Format("{0}r{1}", Common.Misc.GetUnixTimeStamp(), rnd.Next(0, 1024));

            string file_path = Path.Combine(Paths.PhysicalFilesStorageFolder, chanb_name + "." + file_extension);

            file.SaveAs(file_path);

            string thumb_path = "";

            switch (file_extension)
            {
            case "jpg":
            case "jpeg":
            case "png":
            case "bmp":
            case "gif":
            case "apng":

                System.Drawing.Image im = null;
                try
                {
                    im = System.Drawing.Image.FromStream(file.InputStream);
                }
                catch (Exception)
                {
                    File.Delete(file_path);
                    throw new Exception("Bad image data");
                }

                bool is_transparent = (file_extension == "png" || file_extension == "apng");

                System.Drawing.Imaging.ImageFormat format = null;

                if (is_transparent)
                {
                    thumb_path = Path.Combine(Paths.PhysicalThumbStorageFolder, chanb_name + ".png");
                    format     = System.Drawing.Imaging.ImageFormat.Png;
                }
                else
                {
                    thumb_path = Path.Combine(Paths.PhysicalThumbStorageFolder, chanb_name + ".jpg");
                    format     = System.Drawing.Imaging.ImageFormat.Jpeg;
                }

                if ((im.Width * im.Height) <= 62500)
                {
                    im.Save(thumb_path, format);
                }
                else
                {
                    using (System.Drawing.Image rezized = ImageManipulator.ResizeImage(im))
                    {
                        rezized.Save(thumb_path, format);
                    }
                }

                if (format == System.Drawing.Imaging.ImageFormat.Png)
                {
                    UnmanagedWrappers.FileOptimizer.OptimizePNG(thumb_path);
                }
                else
                {
                    UnmanagedWrappers.FileOptimizer.OptimizeJPG(thumb_path);
                }

                WPostFile wpf = new WPostFile()
                {
                    ChanbName  = chanb_name + "." + file_extension,
                    Size       = file.ContentLength,
                    Dimensions = string.Format("{0}x{1}", im.Width, im.Height),
                    Extension  = file_extension.ToUpper(),
                    RealName   = file.FileName,
                    Hash       = file_hash,
                    PostID     = postID,
                    MimeType   = file.ContentType   // HACK
                };

                im.Dispose();

                AddFileToDatabase(wpf, con);

                break;

            case "webm":

                if (!Validator.ValidateWEBM(file.InputStream))
                {
                    File.Delete(file_path);
                    throw new Exception("Invalid WEBM file");
                }

                UnmanagedWrappers.FileOptimizer.OptimizeWEBM(file_path);

                string optimized_webm = Path.Combine(Paths.PhysicalFilesStorageFolder, "clean." + chanb_name + "." + file_extension);

                if (File.Exists(optimized_webm))
                {
                    File.Delete(file_path);               //delete the unoptimized file
                    File.Move(optimized_webm, file_path); // replace the file with the optimized one
                }

                thumb_path = Path.Combine(Paths.PhysicalThumbStorageFolder, chanb_name + ".png");

                System.Drawing.Image thumb = UnmanagedWrappers.FFMpegWrapper.GenerateWEBMThumb(file_path);

                if (thumb != null)
                {
                    if (thumb.Width * thumb.Width <= 62500)
                    {
                        thumb.Save(thumb_path, System.Drawing.Imaging.ImageFormat.Png);
                    }
                    else
                    {
                        using (System.Drawing.Image rezized = ImageManipulator.ResizeImage(thumb))
                        {
                            rezized.Save(thumb_path, System.Drawing.Imaging.ImageFormat.Png);
                        }
                    }

                    UnmanagedWrappers.FileOptimizer.OptimizePNG(thumb_path);
                }

                WPostFile wpf_webm = new WPostFile()
                {
                    ChanbName  = chanb_name + "." + file_extension,
                    Size       = file.ContentLength,
                    Dimensions = string.Format("{0}x{1}", (thumb == null) ? 50 : thumb.Width, (thumb == null) ? 25 : thumb.Height),
                    Extension  = file_extension.ToUpper(),
                    RealName   = file.FileName,
                    Hash       = file_hash,
                    PostID     = postID,
                    MimeType   = file.ContentType   // HACK
                };

                if (thumb != null)
                {
                    thumb.Dispose();
                }

                AddFileToDatabase(wpf_webm, con);

                break;

            case "pdf":
                thumb_path = Path.Combine(Paths.PhysicalThumbStorageFolder, chanb_name + ".jpg");

                System.Drawing.Image thumb_pdf = UnmanagedWrappers.GhostScriptWrapper.GeneratePDFThumb(file_path);

                if (thumb_pdf != null)
                {
                    if (thumb_pdf.Width * thumb_pdf.Width <= 62500)
                    {
                        thumb_pdf.Save(thumb_path, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                    else
                    {
                        using (System.Drawing.Image rezized = ImageManipulator.ResizeImage(thumb_pdf))
                        {
                            rezized.Save(thumb_path, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                    }

                    UnmanagedWrappers.FileOptimizer.OptimizeJPG(thumb_path);
                }

                WPostFile wpf_pdf = new WPostFile()
                {
                    ChanbName  = chanb_name + "." + file_extension,
                    Size       = file.ContentLength,
                    Dimensions = string.Format("{0}x{1}", (thumb_pdf == null) ? 50 : thumb_pdf.Width, (thumb_pdf == null) ? 25 : thumb_pdf.Height),
                    Extension  = file_extension.ToUpper(),
                    RealName   = file.FileName,
                    Hash       = file_hash,
                    PostID     = postID,
                    MimeType   = file.ContentType   // HACK
                };

                if (thumb_pdf != null)
                {
                    thumb_pdf.Dispose();
                }

                AddFileToDatabase(wpf_pdf, con);
                break;

            default:
                throw new Exception(string.Format("Unsupported file type '{0}'", file_extension));
            }
        }