Exemplo n.º 1
0
        public ActionResult Add()
        {
            _logger.Info("DocumentController 添加文档");

            try
            {
                var fileCollection = _requestProvider.FileCollection;

                var userId    = _contextService.UserId;
                var userName  = _contextService.NickName;
                var depId     = _contextService.DepId;
                var timeStamp = _requestProvider["TimeStamp"];
                var spaceId   = _requestProvider["SpaceId"];

                var space = string.IsNullOrWhiteSpace(spaceId) ? _spaceService.GetDefaultSpace() : _spaceService.GetSpace(spaceId);

                _logger.Info(string.Format("Add Document, spaceId:[{0}];spaceSeqNo:[{1}], spaceName:[{2}], userId:[{3}];userName:[{4}]。",
                                           space.Id, space.SpaceSeqNo, space.SpaceName, userId, userName));



                var documents = new List <DocumentObject>();

                for (int i = 0; i < fileCollection.Count; ++i)
                {
                    var document = DocumentBuilder.Build(fileCollection[i], space.Id.ToString(), space.SpaceSeqNo, space.SpaceName, userId, userName, depId, Visible.Public);
                    documents.Add(document);
                    var mimeType = MimeMapping.GetMimeMapping(document.FileName);
                    _storePolicy.AddStream(fileCollection[i].FileStream, mimeType, document.StorePath);
                }


                var contracts = _documentService.Add(documents);
                if (!string.IsNullOrWhiteSpace(timeStamp))
                {
                    MemoryContainer.Push(timeStamp, documents);
                }

                return(Json(new UploadViewModel(ErrorMessages.Success,
                                                ErrorMessages.GetErrorMessages(ErrorMessages.Success),
                                                contracts)));
            }
            catch (Exception ex)
            {
                _logger.Error(ex.StackTrace);
                _logger.Error(ex.Message);
            }

            _logger.Error(ErrorMessages.GetErrorMessages(ErrorMessages.UploadFailed));
            return(Json(new UploadViewModel(ErrorMessages.UploadFailed,
                                            ErrorMessages.GetErrorMessages(ErrorMessages.UploadFailed))));
        }
Exemplo n.º 2
0
        public DocumentContract Add()
        {
            _logger.Info("DocumentController 添加文档.");

            var userId   = _requestProvider["UserId"];
            var spaceId  = _requestProvider["SpaceId"];
            var userName = _requestProvider["UserName"];
            var depId    = _requestProvider["DepId"];
            var visible  = _requestProvider["Visible"];
            var path     = _requestProvider["Path"];

            var timeStamp = _requestProvider["TimeStamp"];

            if (string.IsNullOrWhiteSpace(userId))
            {
                _logger.Error("文档参数错误, 缺少 UserId 参数.");
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            _logger.InfoFormat("文档参数userId:{0}", userId);

            if (string.IsNullOrWhiteSpace(userName))
            {
                _logger.Error("文档参数错误, 缺少 userName 参数.");
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            _logger.InfoFormat("文档参数userName:{0}", userName);

            if (string.IsNullOrWhiteSpace(depId))
            {
                _logger.Error("文档参数缺少 depId 参数,默认设置为空");
                depId = string.Empty;
            }
            _logger.InfoFormat("文档参数depId:{0}", depId);

            var visiblity = Visible.Public;

            if (!string.IsNullOrWhiteSpace(visible))
            {
                visiblity = (Visible)Enum.Parse(typeof(Visible), visible);
                _logger.InfoFormat("文档参数为:{0}", visiblity);
            }
            else
            {
                _logger.Info("文档参数缺少 visible 参数,默认设置为Public");
            }

            SpaceObject space;

            if (string.IsNullOrWhiteSpace(path))
            {
                _logger.Info("文档参数缺少 path 参数,采用spaceId参数");
                space = string.IsNullOrWhiteSpace(spaceId) ? _spaceService.GetDefaultSpace() : _spaceService.GetSpace(spaceId);
            }
            else
            {
                _logger.InfoFormat("文档参数path:{0}", path);
                space = _spaceService.MakeSpace(string.Empty, path, userId, userName, depId, visiblity);
            }

            var fileData = _requestProvider.FileCollection[0];

            if (fileData == null)
            {
                _logger.Error("文档参数错误, fileData ContentLength为0.");
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            _logger.Info(string.Format("Add Document, spaceId:[{0}];userId:[{1}];userName:[{2}]。", spaceId, userId, userName));

            try
            {
                var document = DocumentBuilder.Build(fileData,
                                                     space.Id.ToString(),
                                                     space.SpaceSeqNo,
                                                     space.SpaceName,
                                                     userId,
                                                     userName,
                                                     depId,
                                                     visiblity);
                var mimeType = MimeMapping.GetMimeMapping(document.FileName);
                _storePolicy.AddStream(fileData.FileStream, mimeType, document.StorePath);

                var documentObj = _documentService.Add(document);
                if (documentObj.DocumentCategory != DocumentCategory.Image)
                {
                    documentObj.DisplayPath = documentObj.PreviewUrl;
                }

                var contract = documentObj.ToObject <DocumentContract>();

                if (!string.IsNullOrWhiteSpace(timeStamp))
                {
                    MemoryContainer.Push(timeStamp, contract);
                }

                return(contract);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                _logger.Error(ex.StackTrace);
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }