public bool RemoveImageFile(string fileUD)
        {
            Library.FileHelper.ImageManager imgMng = new Library.FileHelper.ImageManager(FrameworkSetting.Setting.AbsoluteFileFolder, FrameworkSetting.Setting.AbsoluteThumbnailFolder);
            using (FrameworkEntities context = CreateContext())
            {
                try
                {
                    Files _file = context.Files.FirstOrDefault(o => o.FileUD == fileUD);
                    if (_file != null)
                    {
                        if (!string.IsNullOrEmpty(_file.FileLocation))
                        {
                            try
                            {
                                imgMng.DeleteFile(_file.FileLocation);
                            }
                            catch { }
                        }

                        if (!string.IsNullOrEmpty(_file.ThumbnailLocation))
                        {
                            try
                            {
                                imgMng.DeleteFile(_file.ThumbnailLocation);
                            }
                            catch { }
                        }

                        context.Files.Remove(_file);
                        context.SaveChanges();
                    }
                    return(true);
                }
                catch { return(false); }
            }
        }
Beispiel #2
0
        public override bool DeleteData(int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (SubMaterialOptionMngEntities context = CreateContext())
                {
                    SubMaterialOption dbItem = context.SubMaterialOption.FirstOrDefault(o => o.SubMaterialOptionID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "Sub material option not found!";
                        return(false);
                    }
                    else
                    {
                        // processing image
                        if (!string.IsNullOrEmpty(dbItem.ImageFile))
                        {
                            Library.FileHelper.ImageManager imgMng = new Library.FileHelper.ImageManager(FrameworkSetting.Setting.AbsoluteFileFolder, FrameworkSetting.Setting.AbsoluteThumbnailFolder);

                            // delete phisycal files
                            string toBeDeletedFileLocation      = "";
                            string toBeDeletedThumbnailLocation = "";
                            fwFactory.GetDBFileLocation(dbItem.ImageFile, out toBeDeletedFileLocation, out toBeDeletedThumbnailLocation);

                            if (!string.IsNullOrEmpty(toBeDeletedFileLocation))
                            {
                                try
                                {
                                    imgMng.DeleteFile(toBeDeletedFileLocation);
                                }
                                catch { }
                            }

                            if (!string.IsNullOrEmpty(toBeDeletedThumbnailLocation))
                            {
                                try
                                {
                                    imgMng.DeleteFile(toBeDeletedThumbnailLocation);
                                }
                                catch { }
                            }
                        }

                        context.SubMaterialOption.Remove(dbItem);
                        context.SaveChanges();

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }
        public string CreateNoneImageFilePointer(string tmpFolder, string newFile, string oldFilePointer)
        {
            string result = string.Empty;

            Library.FileHelper.ImageManager imgMng = new Library.FileHelper.ImageManager(FrameworkSetting.Setting.AbsoluteFileFolder, FrameworkSetting.Setting.AbsoluteThumbnailFolder);

            using (FrameworkEntities context = CreateContext())
            {
                // delete phisycal files
                Files _file = context.Files.FirstOrDefault(o => o.FileUD == oldFilePointer);
                if (_file != null)
                {
                    if (!string.IsNullOrEmpty(_file.FileLocation))
                    {
                        try
                        {
                            imgMng.DeleteFile(_file.FileLocation);
                        }
                        catch { }
                        _file.FileLocation = string.Empty;
                    }

                    if (!string.IsNullOrEmpty(_file.ThumbnailLocation))
                    {
                        try
                        {
                            imgMng.DeleteFile(_file.ThumbnailLocation);
                        }
                        catch { }
                        _file.ThumbnailLocation = string.Empty;
                    }

                    // remove the file pointer if new file is empty
                    if (string.IsNullOrEmpty(newFile))
                    {
                        // remove file registration in database
                        context.Files.Remove(_file);
                        context.SaveChanges();

                        result = string.Empty;
                    }
                }

                if (!string.IsNullOrEmpty(newFile))
                {
                    // assign new file
                    string outFileFullPath, outDBFileLocation;

                    // copy the new file
                    imgMng.StoreFile(tmpFolder + newFile, out outDBFileLocation, out outFileFullPath);

                    if (File.Exists(outFileFullPath))
                    {
                        FileInfo info = new FileInfo(outFileFullPath);

                        // insert/update file registration in database
                        try
                        {
                            if (_file == null)
                            {
                                _file        = new Files();
                                _file.FileUD = System.Guid.NewGuid().ToString().ToLower().Replace("-", "");
                                context.Files.Add(_file);
                            }
                            _file.FriendlyName  = newFile;
                            _file.FileLocation  = outDBFileLocation;
                            _file.FileExtension = info.Extension;
                            _file.FileSize      = (int)info.Length;

                            context.SaveChanges();

                            result = _file.FileUD;
                        }
                        catch
                        {
                            result = string.Empty;
                        }
                    }
                }
            }

            return(result);
        }