public static CheckResult ValidateSaveView(DSModel db, FileBlobViewModel model)
        {
            CheckResult res = new CheckResult(model);

            if (string.IsNullOrWhiteSpace(model.BlobName))
                res.AddError("File name cannot bet empty!", model.GetName(p => p.BlobName));
            if (string.IsNullOrWhiteSpace(model.BlobExtension))
                res.AddError("File extension cannot be empty!", model.GetName(p => p.BlobExtension));

            return res;
        }
        public static void SaveBlobView(DSModel db, KeyBinder key, FileBlobViewModel model)
        {
            if (db == null)
                throw new ArgumentNullException("db");
            if (key == null)
                throw new ArgumentNullException("key");
            if (model == null)
                throw new ArgumentNullException("model");
            if (model.BlobID == 0)
                throw new ArgumentException("BlobID cannot be 0!", "model");

            string sql = @"
                UPDATE file_blobs f
                SET
                  f.BlobName = @BlobName, f.BlobDescription = @BlobDescription, f.BlobExtension = @BlobExtension
                WHERE
                  f.BlobID = @BlobID;";
            db.ExecuteNonQuery(sql,
                new MySqlParameter("BlobID", model.BlobID),
                new MySqlParameter("BlobName", model.BlobName),
                new MySqlParameter("BlobDescription", model.BlobDescription),
                new MySqlParameter("BlobExtension", model.BlobExtension));
        }
        public CheckResult PreviewFile(FileBlobViewModel model)
        {
            CheckResult res = new CheckResult(model);
            using (var db = DB.GetContext())
            {
                var file = FileBlobRepository.GetBlob(db, model.BlobID);
                if (file == null)
                {
                    res.AddError("No such file!", string.Empty);
                    return res;
                }

                string directory = Path.Combine(Environment.CurrentDirectory, "files");
                if (!Directory.Exists(directory))
                    Directory.CreateDirectory(directory);

                string fileName = Path.Combine(directory, ExtensionMethods.SafeFileName(file.BlobName) + file.BlobExtension);
                if (File.Exists(fileName))
                {
                    try
                    {
                        File.Delete(fileName);
                    }
                    catch (Exception ex)
                    {
                        return new CheckResult(ex);
                    }
                }

                try
                {
                    File.WriteAllBytes(fileName, file.BlobData);
                    ProcessStartInfo info = new ProcessStartInfo(fileName);
                    info.UseShellExecute = true;
                    Process.Start(info);
                    return res;
                }
                catch (Exception ex)
                {
                    return new CheckResult(ex);
                }
            }
        }
        public CheckResult SaveFile(FileBlobViewModel model)
        {
            if (!model.IsChanged)
                return new CheckResult(model);

            using (var db = DB.GetContext())
            {
                var check = FileBlobValidator.ValidateSaveView(db, model);
                if (check.Failed)
                    return check;

                KeyBinder key = new KeyBinder();
                try
                {
                    FileBlobRepository.SaveBlobView(db, key, model);
                    db.SaveChanges();
                    key.BindKeys();
                    model.IsChanged = false;
                    return new CheckResult(model);
                }
                catch (Exception ex)
                {
                    key.RollbackKeys();
                    return new CheckResult(ex);
                }
            }
        }
        public CheckResult DeleteFile(FileBlobViewModel model)
        {
            FileBlobModel mod = new FileBlobModel();
            mod.BlobID = model.BlobID;

            return this.DeleteFile(mod);
        }