public bool RemoveMark(params int[] fileList) { if (fileList.IsNullOrEmpty()) { return(true); } try { using (var db = new Db.DataContext()) using (var scope = db.CreateScope(TransactionScopeOption.Required)) { var query = db.File.Where(x => !x.IsRemoved && !x.IsRemoving).In(fileList, x => x.IdFile); if (query.ForEach(file => { file.IsRemoving = true; }) > 0) { db.SaveChanges(); scope.Complete(); } } return(true); } catch (Exception ex) { this.RegisterEvent(EventType.Error, "Ошибка удаления файлов с истекшим сроком", null, null, ex); return(false); } }
public bool UpdateExpiration(int[] fileList, DateTime?dateExpires = null) { if (fileList == null) { throw new ArgumentNullException(nameof(fileList)); } if (fileList.IsNullOrEmpty()) { return(true); } try { using (var db = new Db.DataContext()) { var query = db.File.Where(x => !x.IsRemoved && !x.IsRemoving).In(fileList, x => x.IdFile); if (query.ForEach(file => file.DateExpire = dateExpires) > 0) { db.SaveChanges(); } } return(true); } catch (Exception ex) { this.RegisterEvent( EventType.Error, "Ошибка обновления срока хранения файлов", $"Идентификаторы файлов: {string.Join(", ", fileList)}.\r\nНовый срок: {dateExpires?.ToString("dd.MM.yyyy HH:mm:ss")}.", null, ex); return(false); } }
public bool UpdateExpiration(int idFile, DateTime?dateExpires = null) { try { using (var db = new Db.DataContext()) { var file = db.File.Where(x => x.IdFile == idFile && !x.IsRemoved && !x.IsRemoving).FirstOrDefault(); if (file != null) { file.DateExpire = dateExpires; db.SaveChanges(); } } return(true); } catch (Exception ex) { this.RegisterEvent( EventType.Error, "Ошибка обновления срока хранения файла", $"Идентификатор файла: {idFile}.\r\nНовый срок: {dateExpires?.ToString("dd.MM.yyyy HH:mm:ss")}.", null, ex); return(false); } }
public bool RemoveCompletely(params int[] fileList) { if (fileList.IsNullOrEmpty()) { return(true); } try { var rootDirectory = AppCore.ApplicationWorkingFolder; using (var db = new Db.DataContext()) using (var scope = db.CreateScope(TransactionScopeOption.RequiresNew)) { var query = db.File.Where(x => !x.IsRemoved && !x.IsRemoving).In(fileList, x => x.IdFile); if (query.ForEach(file => { try { var pathCombined = Path.Combine(rootDirectory, file.PathFile); if (File.Exists(pathCombined)) { File.Delete(pathCombined); } } catch (IOException) { return; } catch (UnauthorizedAccessException) { return; } catch { } db.File.Remove(file); }) > 0) { db.SaveChanges(); scope.Complete(); } } return(true); } catch (Exception ex) { this.RegisterEvent(EventType.Error, "Ошибка удаления файлов с истекшим сроком", null, null, ex); return(false); } }
internal static void RemoveMarkedFiles() { if (_thisModule?.AppCore?.GetState() != CoreComponentState.Started) { return; } if (!_servicesFlags.TryLock("RemoveMarkedFiles")) { return; } int countFiles = 0; try { var executionTimeLimit = TimeSpan.FromSeconds(50); var dateStart = DateTime.Now; int idFileMax = 0; var rootDirectory = _thisModule?.AppCore?.ApplicationWorkingFolder; using (var db = new Db.DataContext()) { while ((DateTime.Now - dateStart) < executionTimeLimit) { var fileToRemoveQuery = (from FileRemoveQueue in db.FileRemoveQueue join File in db.File.AsNoTracking() on FileRemoveQueue.IdFile equals File.IdFile into File_j from File in File_j.DefaultIfEmpty() where FileRemoveQueue.IdFile > idFileMax orderby FileRemoveQueue.IdFile ascending select new { FileRemoveQueue, File }).Take(100); var fileToRemoveList = fileToRemoveQuery.ToList(); if (fileToRemoveList.Count == 0) { break; } var removeList = new List <int>(); var updateList = new List <Db.File>(); fileToRemoveList.ForEach(row => { try { if (row.File == null) { removeList.Add(row.FileRemoveQueue.IdFile); } else { var pathCombined = Path.Combine(rootDirectory, row.File.PathFile); if (File.Exists(pathCombined)) { File.Delete(pathCombined); } removeList.Add(row.FileRemoveQueue.IdFile); row.File.IsRemoving = false; row.File.IsRemoved = true; updateList.Add(row.File); } } catch (IOException) { return; } catch (UnauthorizedAccessException) { return; } catch { } idFileMax = row.FileRemoveQueue.IdFile; }); if (removeList.Count > 0 || updateList.Count > 0) { using (var scope = db.CreateScope(TransactionScopeOption.RequiresNew)) { if (removeList.Count > 0) { var query = db.FileRemoveQueue.In(removeList, x => x.IdFile); db.FileRemoveQueue.RemoveRange(query); db.SaveChanges <Db.FileRemoveQueue>(); } if (updateList.Count > 0) { if (updateList.Any(x => x.IsRemoving || !x.IsRemoved)) { throw new Exception("Флаги удаления сбросились!"); } db.File. UpsertRange(updateList). AllowIdentityMatch(). On(x => x.IdFile). WhenMatched((xDb, xIns) => new Db.File() { IsRemoved = xIns.IsRemoved, IsRemoving = xIns.IsRemoving }). Run(); } scope.Complete(); } countFiles += updateList.Count; } } } } catch (ThreadAbortException) { } catch (Exception ex) { _thisModule?.RegisterEvent(EventType.Error, "Ошибка заполнения очереди удаления", null, ex); } finally { _servicesFlags.ReleaseLock("RemoveMarkedFiles"); if (countFiles > 0) { _thisModule?.RegisterEvent(EventType.Info, "Удаление файлов", $"Удалено {countFiles} файлов.", null); } } }
public RegisterResult Register(out Db.File result, string nameFile, string pathFile, Guid?uniqueKey = null, DateTime?dateExpires = null) { if (string.IsNullOrEmpty(nameFile)) { throw new ArgumentNullException(nameof(nameFile)); } if (string.IsNullOrEmpty(pathFile)) { throw new ArgumentNullException(nameof(pathFile)); } if (Path.GetInvalidFileNameChars().Any(x => nameFile.Contains(x))) { throw new ArgumentException("Содержит символы, не разрешенные в имени файла.", nameof(nameFile)); } result = null; var pathFileFull = Path.Combine(AppCore.ApplicationWorkingFolder, pathFile); if (!File.Exists(pathFileFull)) { return(RegisterResult.NotFound); // throw new FileNotFoundException("Файл не существует", pathFile); } try { var context = AppCore.GetUserContextManager().GetCurrentUserContext(); var pathFileOld = string.Empty; using (var db = new Db.DataContext()) { var data = uniqueKey.HasValue ? (db.File.Where(x => x.UniqueKey == uniqueKey).FirstOrDefault() ?? null) : null; if (data != null && pathFile != data.PathFile) { pathFileOld = data.PathFile; } var isNew = false; if (data == null) { isNew = true; data = new Db.File(); } data.IdModule = 0; data.NameFile = nameFile; data.PathFile = pathFile; data.DateChange = DateTime.Now.Timestamp(); data.DateExpire = dateExpires; data.IdUserChange = context.IdUser; data.UniqueKey = uniqueKey; data.IsRemoved = false; data.IsRemoving = false; var fileInfo = new FileInfo(pathFileFull); var fileType = fileInfo.GetFileType(); data.TypeConcrete = fileType.Mime; if (fileType == MimeTypes.JPEG || fileType == MimeTypes.PNG || fileType == MimeTypes.BMP || fileType == MimeTypes.GIF) { data.TypeCommon = FileTypeCommon.Image; } if (isNew) { db.File.Add(data); } if (db.SaveChanges() > 0) { if (!string.IsNullOrEmpty(pathFileOld)) { var pathFileFullOld = Path.Combine(AppCore.ApplicationWorkingFolder, pathFileOld); if (File.Exists(pathFileFullOld)) { File.Delete(pathFileFullOld); } } } result = data; return(RegisterResult.Success); } } catch (Exception ex) { this.RegisterEvent(EventType.Error, "Ошибка регистрации файла", $"nameFile='{nameFile}'.\r\npathFile='{pathFile}'.\r\nuniqueKey='{uniqueKey}'.\r\ndateExpires={dateExpires?.ToString("dd.MM.yyyy HH:mm:ss")}.", null, ex); return(RegisterResult.Error); } }