Пример #1
0
 public NotFound TryGetFile(int idFile, out Db.File result)
 {
     try
     {
         using (var db = new Db.DataContext())
         {
             result = db.File.Where(x => x.IdFile == idFile && !x.IsRemoved && !x.IsRemoving).FirstOrDefault();
             return(result != null ? NotFound.Success : NotFound.NotFound);
         }
     }
     catch (Exception ex)
     {
         result = null;
         this.RegisterEvent(EventType.Error, "Ошибка получения файла", $"Идентификатор файла: {idFile}.", null, ex);
         return(NotFound.Error);
     }
 }
Пример #2
0
        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);
            }
        }
Пример #3
0
        public NotFound TryGetFile(Expression <Func <Db.File, bool> > searchExpression, out Db.File result)
        {
            if (searchExpression == null)
            {
                throw new ArgumentNullException(nameof(searchExpression));
            }

            try
            {
                using (var db = new Db.DataContext())
                {
                    try
                    {
                        var query = db.File.Where(x => !x.IsRemoved && !x.IsRemoving).Where(searchExpression).FirstOrDefault();
                        result = query;
                    }
                    catch (NotSupportedException)
                    {
                        throw new ArgumentException("Некорректное выражение", nameof(searchExpression));
                    }
                    return(result != null ? NotFound.Success : NotFound.NotFound);
                }
            }
            catch (ArgumentException) { throw; }
            catch (Exception ex)
            {
                result = null;
                this.RegisterEvent(EventType.Error, "Ошибка получения файла", $"Выражение поиска: {searchExpression.ToString()}.", null, ex);
                return(NotFound.Error);
            }
        }