示例#1
0
        public void HandlePutFileRequest(WopiRequest requestData)
        {
            lock (this)
            {
                // userId(user@polihub) will be passed from the policyHub application
                if (!_authorization.ValidateToken(requestData.AccessToken, "user@policyhub", requestData.Id))
                {
                    _errorHandler.ReturnInvalidToken(_response);
                    _response.Close();
                    return;
                }

                if (!File.Exists(requestData.FullPath))
                {
                    _errorHandler.ReturnFileUnknown(_response);
                    _response.Close();
                    return;
                }
                string   newLock = requestData.LockId;
                LockInfo existingLock;
                bool     hasExistingLock;

                lock (LockInfo.Locks)
                {
                    hasExistingLock = LockInfo.TryGetLock(requestData.Id, out existingLock);
                }

                if (hasExistingLock && existingLock.Lock != newLock)
                {
                    // lock mismatch/locked by another interface
                    _errorHandler.ReturnLockMismatch(_response, existingLock.Lock);
                    _response.AddHeader(WopiHeaders.Lock, existingLock.Lock);
                    _response.AddHeader(WopiHeaders.LockFailureReason, "Lock mismatch/Locked by another interface");
                    _response.StatusCode = (int)HttpStatusCode.Conflict;
                    _response.Close();
                    return;
                }

                FileInfo putTargetFileInfo = new FileInfo(requestData.FullPath);

                if (!hasExistingLock && putTargetFileInfo.Length != 0)
                {
                    _response.AddHeader(WopiHeaders.Lock, newLock);
                    _response.AddHeader(WopiHeaders.LockFailureReason, "PutFile on unlocked file with current size != 0");
                    _response.StatusCode = (int)HttpStatusCode.Conflict;
                    _errorHandler.ReturnLockMismatch(_response, reason: "PutFile on unlocked file with current size != 0");
                    _response.Close();
                    return;
                }


                try
                {
                    ResponseGenerator generator = new ResponseGenerator(putTargetFileInfo);

                    generator.Save(requestData.FileData);
                    _response.ContentLength64 = 0;
                    _response.ContentType     = @"text/html";
                    _response.StatusCode      = (int)HttpStatusCode.OK;
                }
                catch (UnauthorizedAccessException)
                {
                    _errorHandler.ReturnFileUnknown(_response);
                }
                catch (IOException)
                {
                    _errorHandler.ReturnServerError(_response);
                }
                _response.Close();
            }
        }