public ApiReturns Post(FileDirectory model)
        {
            // 只有超级管理员才能添加顶级目录
            if (model.ParentId == 0 && !LoginStatus.IsSuperAdminLogin())
            {
                return(ApiReturns.Forbidden());
            }

            if (DirNameExists(model))
            {
                return(ApiReturns.Exists());
            }

            model.CreateTime     = DateTime.Now;
            model.LastModifyTime = DateTime.Now;
            model.CreatorId      = LoginStatus.GetLoginUser().Id;

            // 插入数据库
            var success = _dirBll.ExecuteTranscation(() =>
            {
                var s = _dirBll.Add(model).Id > 0;
                if (s)
                {
                    var d = new DbUpdateLog(nameof(FileDirectory), model.Id, (int)Operation.Insert);
                    return(_logBll.Add(d).Id > 0);
                }
                return(false);
            });

            if (success)
            {
                return(ApiReturns.Created(model));
            }

            return(ApiReturns.BadRequest());
        }
示例#2
0
        public ApiReturns Post()
        {
            var request     = HttpContext.Current.Request;
            var directoryId = request["dir"].ToInt32();

            if (directoryId <= 0)
            {
                return(ApiReturns.BadRequest());
            }

            var directory = _dirBll.QuerySingle(directoryId);

            if (directory == null)
            {
                return(ApiReturns.BadRequest());
            }

            var files = request.Files;

            if (files.Count > 0)
            {
                var loginUser    = LoginStatus.GetLoginUser();
                var fileIsCommom = LoginStatus.IsSuperAdminLogin() && directory.IsCommon;

                var file       = files[0];
                var fileName   = Path.GetFileNameWithoutExtension(file.FileName);
                var departFile = new DepartFiles
                {
                    DepartmentId    = LoginStatus.IsSuperAdminLogin() ? 0 : loginUser.DepartmentId.Value,
                    FileDirectoryId = directoryId, FileName = fileName, IsCommon = fileIsCommom, SystemUserId = loginUser.Id, LastModifyTime = DateTime.Now
                };

                #region   的文件已存在于服务器
                // 对比哈希值,相同文件只保存一份副本
                var hash   = file.InputStream.GetMd5();
                var dbFile = _fileInfoBll.QuerySingle(f => f.IsDeleted == false && f.HashCode == hash);
                if (dbFile != null)
                {
                    departFile.FileInfoId = dbFile.Id;

                    var addSuccess = _departFilesBll.ExecuteTranscation(() =>
                    {
                        if (_departFilesBll.Add(departFile).Id > 0)
                        {
                            var log = new DbUpdateLog
                            {
                                OperateType = (int)Operation.Insert,
                                TableName   = nameof(DepartFiles),
                                TargetId    = departFile.Id,
                                UpdateTime  = DateTime.Now
                            };
                            return(_dbUpdateLogBll.Add(log).Id > 0);
                        }

                        return(false);
                    });
                    var res = _fileBll.QuerySingle(departFile.Id);

                    if (addSuccess)
                    {
                        return(ApiReturns.Created(res));
                    }

                    return(ApiReturns.BadRequest());
                }
                #endregion

                #region   的文件未存在于服务器
                // 路径
                var d    = DateTime.Now;
                var ext  = Path.GetExtension(file.FileName);
                var dir  = $"/docs/{d.Year}-{d.Month}-{d.Day}/";
                var name = $"{Guid.NewGuid()}{ext}";
                var path = $"{WebConfig.FileUploadDir}{dir}";
                if (!Directory.Exists(path))
                {
                    lock (_lockObj)
                    {
                        Directory.CreateDirectory(path);
                    }
                }

                // 保存文件到磁盘
                file.SaveAs($"{path}{name}");

                // 保存到数据库
                var serverFileName = $"{dir}{name}";
                dbFile = new Guoli.Fs.Model.FileInfo
                {
                    Extension  = ext,
                    HashCode   = hash,
                    Path       = serverFileName,
                    Size       = file.InputStream.Length,
                    UploadTime = d
                };

                var success = _fileInfoBll.ExecuteTranscation(() =>
                {
                    var s = _fileInfoBll.Add(dbFile).Id > 0;
                    if (s)
                    {
                        departFile.FileInfoId = dbFile.Id;

                        if (_departFilesBll.Add(departFile).Id > 0)
                        {
                            var log = new DbUpdateLog
                            {
                                OperateType = (int)Operation.Insert,
                                TableName   = nameof(DepartFiles),
                                TargetId    = departFile.Id,
                                UpdateTime  = DateTime.Now
                            };
                            return(_dbUpdateLogBll.Add(log).Id > 0);
                        }

                        return(false);
                    }

                    return(false);
                });

                if (success)
                {
                    var data = _fileBll.QuerySingle(departFile.Id);
                    return(ApiReturns.Created(data));
                }

                return(ApiReturns.BadRequest());

                #endregion
            }

            return(ApiReturns.BadRequest());
        }