Example #1
0
        public WopiResponse Handle()
        {
            var requestData = WopiRequest.ParseRequest(_request);

            try
            {
                var documentId = Convert.ToInt64(requestData.Id);
                var document   = FB.GetDocument(documentId);

                if (document == null)
                {
                    return(new WopiResponse()
                    {
                        ResponseType = WopiResponseType.FileUnknown, Message = "File not found"
                    });
                }

                var currentLock = "";
                if (LockHelper.IsLockMismatch(_request, document, out currentLock))
                {
                    return(new WopiResponse()
                    {
                        ResponseType = WopiResponseType.LockMismatch, Message = currentLock
                    });
                }

                // FileInfo putTargetFileInfo = new FileInfo(requestData.FullPath);

                // The WOPI spec allows for a PutFile to succeed on a non-locked file if the file is currently zero bytes in length.
                // This allows for a more efficient Create New File flow that saves the Lock roundtrips.
                //if (!hasExistingLock && putTargetFileInfo.Length != 0)
                //{
                //    // With no lock and a non-zero file, a PutFile could potentially result in data loss by clobbering
                //    // existing content.  Therefore, return a lock mismatch error.
                //    ReturnLockMismatch(context.Response, reason: "PutFile on unlocked file with current size != 0");
                //}

                // Either the file has a valid lock that matches the lock in the request, or the file is unlocked
                // and is zero bytes.  Either way, proceed with the PutFile.

                var binary = IOHelper.StreamToBytes(_request.InputStream);
                FB.SaveDocument(document, binary);
            }
            catch (UnauthorizedAccessException uex)
            {
                return(new WopiResponse()
                {
                    ResponseType = WopiResponseType.FileUnknown, Message = uex.Message
                });
            }
            catch (Exception ex)
            {
                return(new WopiResponse()
                {
                    ResponseType = WopiResponseType.ServerError, Message = ex.Message
                });
            }

            return(new WopiResponse()
            {
                ResponseType = WopiResponseType.Success
            });
        }
Example #2
0
        public WopiJsonResponse Handle()
        {
            var requestData = WopiRequest.ParseRequest(_request);

            var responseData = new PutRelativeResponse();

            var documentId      = Convert.ToInt64(requestData.Id);
            var relativeTarget  = _request.Headers[WopiHeaders.RelativeTarget];
            var suggestedTarget = _request.Headers[WopiHeaders.SuggestedTarget];

            // make sure we don't have both headers present:
            if (!string.IsNullOrEmpty(relativeTarget) && !string.IsNullOrEmpty(suggestedTarget))
            {
                return(new WopiJsonResponse()
                {
                    StatusCode = 501,
                    Json = new PutRelativeResponse()
                });
            }

            var    overwriteRelative = _request.Headers[WopiHeaders.OverwriteRelativeTarget];
            string extension;

            if (!string.IsNullOrEmpty(relativeTarget))
            {
                // check if we have a file matching the target name
                // and if so, return 409 conflict w/ lock response
                extension         = relativeTarget.Substring(relativeTarget.LastIndexOf(".") + 1);
                responseData.Name = IOHelper.Utf7Encode(relativeTarget); // extension should already be here, we just need to get it for below

                var overwriteExisting = !string.IsNullOrEmpty(overwriteRelative) && overwriteRelative.ToLower().Equals("true");
                var relativeDocument  = FB.GetDocumentByNameAndExtension(responseData.Name, extension, documentId);

                // does this document already exist?
                if (relativeDocument != null)
                {
                    // lock check - make sure the existing document isn't locked:
                    var currentLock = "";
                    if (LockHelper.IsLockMismatch(_request, relativeDocument, out currentLock))
                    {
                        return(new WopiJsonResponse()
                        {
                            StatusCode = 409, Json = responseData
                        });
                    }

                    // not locked - but the document exists, so make sure the overwrite existing header is set:
                    if (!overwriteExisting)
                    {
                        return(new WopiJsonResponse()
                        {
                            StatusCode = 409,
                            Json = responseData
                        });
                    }
                }
            }
            else
            {
                // suggested mode:
                // save the file with whatever name we want, and return that name:
                extension         = suggestedTarget.Substring(suggestedTarget.LastIndexOf(".") + 1);
                responseData.Name = "wopitest_putrelative." + extension;
            }

            var binary        = IOHelper.StreamToBytes(_request.InputStream);
            var newDocumentId = FB.SaveNewDocument(binary, extension, responseData.Name, documentId);
            var newToken      = FB.GetAccessToken(newDocumentId);

            responseData.Url = $"{Constants.WopiApiUrl}wopi/files/{newDocumentId}?access_token={newToken}";

            return(new WopiJsonResponse()
            {
                StatusCode = 200,
                Json = responseData
            });
        }
Example #3
0
        public WopiJsonResponse Handle()
        {
            var requestData = WopiRequest.ParseRequest(_request);
            var newName     = _request.Headers[WopiHeaders.RequestedName];

            try
            {
                // if the file name is illegal then
                // return 400
                // add response header WopiHeaders.InvalidFileNameError - describing the reason the rename operation could not be completed
                if (string.IsNullOrEmpty(newName))
                {
                    return(new WopiJsonResponse()
                    {
                        StatusCode = 400,
                        Headers = new NameValueCollection {
                            { WopiHeaders.InvalidFileNameError, "Invalid filename for rename" }
                        },
                    });
                }

                newName = IOHelper.Utf7Encode(newName);

                var documentId = Convert.ToInt64(requestData.Id);
                var document   = FB.GetDocument(documentId);

                if (document == null)
                {
                    return(new WopiJsonResponse()
                    {
                        StatusCode = 404, Json = new RenameResponse()
                    });
                }

                var currentLock = "";
                if (LockHelper.IsLockMismatch(_request, document, out currentLock))
                {
                    return(new WopiJsonResponse()
                    {
                        StatusCode = 409, Json = new RenameResponse()
                    });
                }

                // rename the document:
                FB.RenameDocument(document, newName);
            }
            catch (Exception ex)
            {
                //return new WopiResponse() { ResponseType = WopiResponseType.ServerError, Message = ex.Message };
                return(new WopiJsonResponse()
                {
                    StatusCode = 500, Json = new { Message = ex.Message }
                });
            }

            //return new WopiResponse() { ResponseType = WopiResponseType.Success };
            return(new WopiJsonResponse()
            {
                StatusCode = 200, Json = new RenameResponse()
                {
                    Name = newName
                }
            });
        }