private static void UpdateBlob(DSModel db, KeyBinder key, FileBlobModel model)
 {
     FileBlob poco = db.FileBlobs.Where(b => b.BlobID == model.BlobID).FirstOrDefault();
     if (poco == null)
         throw new ArgumentException("File does not exist!", "BlobID");
     model.Map(poco);
 }
        public CheckResult SaveFile(FileBlobModel model)
        {
            if (!model.IsChanged)
                return new CheckResult(model);

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

                KeyBinder key = new KeyBinder();
                try
                {
                    FileBlobRepository.SaveBlob(db, key, model);
                    db.SaveChanges();
                    key.BindKeys();
                    model.IsChanged = false;
                    return new CheckResult(model);
                }
                catch (Exception ex)
                {
                    key.RollbackKeys();
                    return new CheckResult(ex);
                }
            }
        }
 public FileBlobViewModel(FileBlobModel model)
 {
     this.BlobID = model.BlobID;
     this.BlobName = model.BlobName;
     this.BlobDescription = model.BlobDescription;
     this.BlobExtension = model.BlobExtension;
     this.BlobSize = ((model.BlobData.Length / 1024) / 1024) + " MB";
 }
示例#4
0
 public FileBlobViewModel(FileBlobModel model)
 {
     this.BlobID          = model.BlobID;
     this.BlobName        = model.BlobName;
     this.BlobDescription = model.BlobDescription;
     this.BlobExtension   = model.BlobExtension;
     this.BlobSize        = ((model.BlobData.Length / 1024) / 1024) + " MB";
 }
        private static void InsertBlob(DSModel db, KeyBinder key, FileBlobModel model)
        {
            key.AddRollback(model.BlobID, model, model.GetName(p => p.BlobID));
            FileBlob poco = new FileBlob();
            model.Map(poco);
            db.Add(poco);

            key.AddKey(poco, model, model.GetName(p => p.BlobID));
        }
        public static void DeleteBlob(DSModel db, KeyBinder key, FileBlobModel model)
        {
            if (db == null)
                throw new ArgumentNullException("db");
            if (key == null)
                throw new ArgumentNullException("key");
            if (model == null)
                throw new ArgumentNullException("model");

            db.FileBlobs
                .Where(b => b.BlobID == model.BlobID)
                .DeleteAll();
        }
        public static CheckResult ValidateSave(DSModel db, FileBlobModel 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));
            if (model.BlobData == null || model.BlobData.Length == 0)
                res.AddError("File data cannot be empty!", model.GetName(p => p.BlobData));

            return res;
        }
        public static void SaveBlob(DSModel db, KeyBinder key, FileBlobModel model)
        {
            if (db == null)
                throw new ArgumentNullException("db");
            if (key == null)
                throw new ArgumentNullException("key");
            if (model == null)
                throw new ArgumentNullException("model");

            model.UserID = GLOB.User.UserID;
            model.LastUpdateTime = DateTime.Now;
            if (model.BlobID == 0)
                InsertBlob(db, key, model);
            else
                UpdateBlob(db, key, model);
        }
        public static CheckResult ValidateDelete(DSModel db, FileBlobModel model)
        {
            CheckResult res = new CheckResult(model);

            return res;
        }
        public CheckResult AttachMultipleFiles(string[] files)
        {
            using (var db = DB.GetContext())
            {
                KeyBinder key = new KeyBinder();
                try
                {
                    foreach (string file in files)
                    {
                        FileBlobModel mod = new FileBlobModel();
                        mod.BlobName = Path.GetFileNameWithoutExtension(file);
                        mod.BlobExtension = Path.GetExtension(file);
                        mod.BlobData = File.ReadAllBytes(file);
                        mod.DriverID = this.CreationInfo.DriverID;

                        FileBlobRepository.SaveBlob(db, key, mod);
                    }

                    db.SaveChanges();
                    key.BindKeys();
                    return new CheckResult();
                }
                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);
        }