Ejemplo n.º 1
0
 public NotFound TryGetFile(int idFile, out DB.File result)
 {
     try
     {
         using (var db = this.CreateUnitOfWork())
         {
             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);
     }
 }
Ejemplo n.º 2
0
        public RegisterResult Register(out DB.File result, string nameFile, string pathFile, string 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();
                if (!string.IsNullOrEmpty(uniqueKey))
                {
                    uniqueKey = uniqueKey.Trim();
                }

                var pathFileOld = string.Empty;
                using (var db = this.CreateUnitOfWork())
                {
                    var data = !string.IsNullOrEmpty(uniqueKey) ? (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;

                    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);
            }
        }
Ejemplo n.º 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 = this.CreateUnitOfWork())
                {
                    try
                    {
                        var query = db.File.Where(x => !x.IsRemoved && !x.IsRemoving).Where(searchExpression).FirstOrDefault();
                        result = query;
                    }
                    catch (NotSupportedException ex)
                    {
                        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);
            }
        }