示例#1
0
        private string GenerateAutoFileName(SavingFileEventArgs args)
        {
            var Extention = Path.GetExtension(args.FileName);
            var name      = Path.GetFileNameWithoutExtension(args.FileName);
            var filename  = name + Extention;

            var p = Path.Combine(args.Path, args.FileName);

            if (!Directory.Exists(args.Path))
            {
                Directory.CreateDirectory(args.Path);
            }
            if (Directory.GetFiles(args.Path, Path.GetFileNameWithoutExtension(args.FileName) + ".*").Any())
            {
                var num = 0;

                do
                {
                    num++;
                    filename = name + "_" + num.ToString();
                    p        = Path.Combine(args.Path, filename) + Extention;
                } while (Directory.GetFiles(args.Path, Path.GetFileNameWithoutExtension(filename) + ".*").Any());
            }

            args.AppPath += (args.AppPath.EndsWith("/") ? "" : "/") + filename;
            return(p);
        }
示例#2
0
        public void GenerateFileName(SavingFileEventArgs args)
        {
            var extention = Path.GetExtension(args.FileName);

            args.Path = Tools.GetPath(args.Path);

            switch (SaveSetting.GenerateFileName)
            {
            case Config.FilenameType.GUID:
                args.SaveAs = GenerateGUIDFileName(args);
                break;

            case Config.FilenameType.Overwrite:
                args.SaveAs   = Path.Combine(args.Path, args.FileName);
                args.AppPath += (args.AppPath.EndsWith("/") ? "" : "/") + args.FileName;
                break;

            case Config.FilenameType.Auto:
                args.SaveAs = GenerateAutoFileName(args);
                break;

            case Config.FilenameType.Custom:
                SaveSetting.GetFilenameGenerator().GenerateFileName(args);
                args.Path = Path.GetDirectoryName(args.SaveAs);
                break;
            }
        }
示例#3
0
        private string GenerateGUIDFileName(SavingFileEventArgs args)
        {
            var Extention = Path.GetExtension(args.FileName);
            var filename  = Guid.NewGuid().ToString() + Extention;
            var p         = Path.Combine(args.Path, filename);

            if (File.Exists(p))
            {
                p = GenerateGUIDFileName(args);
            }
            else
            {
                args.AppPath += (args.AppPath.EndsWith("/") ? "" : "/") + filename;
            }

            return(p);
        }
示例#4
0
        protected virtual void Saving(SavingFileEventArgs args)
        {
            if (File.Exists(args.SaveAs))
            {
                File.Delete(args.SaveAs);
            }

            if (!Directory.Exists(args.Path))
            {
                Directory.CreateDirectory(args.Path);
            }
            using (var stream = File.Open(args.SaveAs, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) {
                var buffer = new byte[1024];
                var readed = args.Stream.Read(buffer, 0, 1024);

                while (readed > 0)
                {
                    stream.Write(buffer, 0, readed);
                    readed = args.Stream.Read(buffer, 0, 1024);
                }
            }
        }
示例#5
0
        private void SaveImage(SavingFileEventArgs args, out bool KeepFile)
        {
            var path     = args.Path;
            var filename = Path.GetFileNameWithoutExtension(args.SaveAs);

            KeepFile = false;

            try {
                using (var mbit = new Bitmap(args.SaveAs)) {
                    foreach (var item in SaveSetting.ImageSetting)
                    {
                        if (item.SaveImmidately)
                        {
                            SaveImage(mbit, path, filename, item);
                        }
                        else
                        {
                            KeepFile = true;
                        }
                    }
                }
            } catch { }
        }
示例#6
0
        public string Save(Stream stream, string fileName)
        {
            var validate = Validate(stream, fileName);

            if (validate.Count > 0)
            {
                validate.ThrowException();
            }

            var args = new SavingFileEventArgs(fileName, stream)
            {
                Path    = SaveSetting.Path,
                Handled = false,
                AppPath = SaveSetting.Path
            };

            GenerateFileName(args);
            Saving(args);

            var keepFile = false;

            if (SaveSetting.CheckImageSetting)
            {
                SaveImage(args, out keepFile);
            }

            if (!keepFile)
            {
                if (!SaveSetting.KeepOrginalImage && SaveSetting.CheckImageSetting)
                {
                    File.Delete(args.SaveAs);
                }
            }

            return(Path.GetFileName(args.SaveAs));
        }
 public abstract void GenerateFileName(SavingFileEventArgs args);
示例#8
0
        public static void CheckFile(string SettingName, string SaveSettingName, string ImageSettingName, string Filename, Stream Output)
        {
            var uploader = new SaveUploadFile(SettingName, SaveSettingName);
            var args     = new SavingFileEventArgs(Filename, null)
            {
                AppPath = uploader.SaveSetting.Path
            };

            uploader.GenerateFileName(args);
            if (!File.Exists(args.SaveAs))
            {
                try {
                    if (!Directory.Exists(args.Path))
                    {
                        Directory.CreateDirectory(args.Path);
                    }

                    var fpath    = Tools.GetPath(args.AppPath);
                    var files    = Directory.GetFiles(Path.GetDirectoryName(fpath), Path.GetFileNameWithoutExtension(args.FileName) + ".*");
                    var filename = files.Single();
                    if (!File.Exists(filename))
                    {
                        return;
                    }

                    using (var bit = new Bitmap(filename)) {
                        var item = uploader.SaveSetting.ImageSetting[ImageSettingName];
                        var dir  = args.Path;
                        var file = Path.Combine(dir, Path.GetFileNameWithoutExtension(Filename)) + "." + item.SaveAs.ToString();

                        if (Output == null)
                        {
                            if (!File.Exists(file))
                            {
                                using (var nbit = uploader.DrawToBitmap(bit, item)) {
                                    if (!Directory.Exists(dir))
                                    {
                                        Directory.CreateDirectory(dir);
                                    }
                                    nbit.Save(file, ConvertToImageFormat(item.SaveAs));
                                }
                            }
                        }
                        else
                        {
                            if (!File.Exists(file))
                            {
                                var open   = File.OpenRead(file);
                                var buffer = new byte[2048];
                                var readed = open.Read(buffer, 0, 2048);

                                while (readed > 0)
                                {
                                    Output.Write(buffer, 0, readed);
                                    readed = open.Read(buffer, 0, 2048);
                                }
                            }
                            else
                            {
                                using (var nbit = uploader.DrawToBitmap(bit, item)) {
                                    nbit.Save(Output, ConvertToImageFormat(item.SaveAs));
                                }
                            }
                        }
                    }
                } catch { }
            }
        }