Ejemplo n.º 1
0
        /// <summary>
        /// Get the file names with link on it
        /// </summary>
        /// <returns>The file names with link</returns>
        public List <FileLink> GetFiles()
        {
            List <FileLink> files = new List <FileLink>();

            IFileStorage  storage   = FileStorageFactory.CreateFileStorage();
            List <string> fileNames = storage.GetFileNames();

            foreach (string fileName in fileNames)
            {
                FileLink fileLink = new FileLink();
                fileLink.Name = fileName;
                if (fileName == ".wopitest")
                {
                    fileLink.Url = string.Format("http://{0}/WopiValidator/Index/{1}",
                                                 ConfigurationManager.AppSettings["WOPIServerName"],
                                                 fileName);
                }

                else
                {
                    fileLink.Url = string.Format("http://{0}/wopiframe/Index/{1}",
                                                 ConfigurationManager.AppSettings["WOPIServerName"],
                                                 fileName);
                }

                files.Add(fileLink);
            }

            return(files);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Processes a CheckFileInfo request
        /// </summary>
        /// <remarks>
        /// For full documentation on CheckFileInfo, see
        /// https://wopi.readthedocs.io/projects/wopirest/en/latest/files/CheckFileInfo.html
        /// </remarks>
        private void HandleCheckFileInfoRequest(HttpContext context, WopiRequest requestData)
        {
            if (!ValidateAccess(requestData, writeAccessRequired: false))
            {
                ReturnInvalidToken(context.Response);
                return;
            }

            IFileStorage storage = FileStorageFactory.CreateFileStorage();
            long         size    = storage.GetFileSize(requestData.Id);

            if (size == -1)
            {
                ReturnFileUnknown(context.Response);
                return;
            }

            bool bRO = storage.GetReadOnlyStatus(requestData.Id);

            try
            {
                CheckFileInfoResponse responseData = new CheckFileInfoResponse()
                {
                    // required CheckFileInfo properties
                    BaseFileName = Path.GetFileName(requestData.Id),
                    OwnerId      = "documentOwnerId",
                    Size         = Convert.ToInt32(size),
                    Version      = storage.GetFileVersion(requestData.Id),
                    UserId       = "WOPITestUser",

                    // optional CheckFileInfo properties
                    BreadcrumbBrandName = "LocalStorage WOPI Host",
                    //BreadcrumbFolderName = fileInfo.Directory != null ? fileInfo.Directory.Name : "",
                    BreadcrumbFolderName = "",
                    BreadcrumbDocName    = Path.GetFileNameWithoutExtension(requestData.Id),
                    BreadcrumbBrandUrl   = "http://" + context.Request.Url.Host,
                    BreadcrumbFolderUrl  = "http://" + context.Request.Url.Host,

                    UserFriendlyName = "A WOPI User",

                    SupportsLocks           = true,
                    SupportsUpdate          = true,
                    UserCanNotWriteRelative = true, /* Because this host does not support PutRelativeFile */

                    ReadOnly     = bRO,
                    UserCanWrite = !bRO
                };

                string jsonString = JsonConvert.SerializeObject(responseData);

                context.Response.Write(jsonString);
                ReturnSuccess(context.Response);
            }
            catch (UnauthorizedAccessException)
            {
                ReturnFileUnknown(context.Response);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Processes a Lock request
        /// </summary>
        /// <remarks>
        /// For full documentation on Lock, see
        /// https://wopi.readthedocs.io/projects/wopirest/en/latest/files/Lock.html
        /// </remarks>
        private void HandleLockRequest(HttpContext context, WopiRequest requestData)
        {
            if (!ValidateAccess(requestData, writeAccessRequired: true))
            {
                ReturnInvalidToken(context.Response);
                return;
            }

            IFileStorage storage = FileStorageFactory.CreateFileStorage();
            long         size    = storage.GetFileSize(requestData.Id);

            if (size == -1)
            {
                ReturnFileUnknown(context.Response);
                return;
            }

            string newLock = context.Request.Headers[WopiHeaders.Lock];

            lock (Locks)
            {
                LockInfo existingLock;
                bool     fLocked = TryGetLock(requestData.Id, out existingLock);
                if (fLocked && existingLock.Lock != newLock)
                {
                    // There is a valid existing lock on the file and it doesn't match the requested lockstring.

                    // This is a fairly common case and shouldn't be tracked as an error.  Office Online can store
                    // information about a current session in the lock value and expects to conflict when there's
                    // an existing session to join.
                    ReturnLockMismatch(context.Response, existingLock.Lock);
                }
                else
                {
                    // The file is not currently locked or the lock has already expired

                    if (fLocked)
                    {
                        Locks.Remove(requestData.Id);
                    }

                    // Create and store new lock information
                    // TODO: In a real implementation the lock should be stored in a persisted and shared system.
                    Locks[requestData.Id] = new LockInfo()
                    {
                        DateCreated = DateTime.UtcNow, Lock = newLock
                    };

                    context.Response.AddHeader(WopiHeaders.ItemVersion, storage.GetFileVersion(requestData.FullPath));

                    // Return success
                    ReturnSuccess(context.Response);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Processes a UnlockAndRelock request
        /// </summary>
        /// <remarks>
        /// For full documentation on UnlockAndRelock, see
        /// https://wopi.readthedocs.io/projects/wopirest/en/latest/files/UnlockAndRelock.html
        /// </remarks>
        private void HandleUnlockAndRelockRequest(HttpContext context, WopiRequest requestData)
        {
            if (!ValidateAccess(requestData, writeAccessRequired: true))
            {
                ReturnInvalidToken(context.Response);
                return;
            }

            IFileStorage storage = FileStorageFactory.CreateFileStorage();
            long         size    = storage.GetFileSize(requestData.Id);

            if (size == -1)
            {
                ReturnFileUnknown(context.Response);
                return;
            }

            string newLock = context.Request.Headers[WopiHeaders.Lock];
            string oldLock = context.Request.Headers[WopiHeaders.OldLock];

            lock (Locks)
            {
                LockInfo existingLock;
                if (TryGetLock(requestData.Id, out existingLock))
                {
                    if (existingLock.Lock == oldLock)
                    {
                        // There is a valid lock on the file and the existing lock matches the provided one

                        // Replace the existing lock with the new one
                        Locks[requestData.Id] = new LockInfo()
                        {
                            DateCreated = DateTime.UtcNow, Lock = newLock
                        };
                        context.Response.Headers[WopiHeaders.OldLock] = newLock;
                        ReturnSuccess(context.Response);
                    }
                    else
                    {
                        // The existing lock doesn't match the requested one.  Return a lock mismatch error
                        // along with the current lock
                        ReturnLockMismatch(context.Response, existingLock.Lock);
                    }
                }
                else
                {
                    // The requested lock does not exist.  That's also a lock mismatch error.
                    ReturnLockMismatch(context.Response, reason: "File not locked");
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Processes a GetFile request
        /// </summary>
        /// <remarks>
        /// For full documentation on GetFile, see
        /// https://wopi.readthedocs.io/projects/wopirest/en/latest/files/GetFile.html
        /// </remarks>
        private void HandleGetFileRequest(HttpContext context, WopiRequest requestData)
        {
            if (!ValidateAccess(requestData, writeAccessRequired: false))
            {
                ReturnInvalidToken(context.Response);
                return;
            }

            IFileStorage storage = FileStorageFactory.CreateFileStorage();
            Stream       stream  = storage.GetFile(requestData.Id);

            if (null == stream)
            {
                ReturnFileUnknown(context.Response);
                return;
            }

            try
            {
                int         i     = 0;
                List <byte> bytes = new List <byte>();
                do
                {
                    byte[] buffer = new byte[1024];
                    i = stream.Read(buffer, 0, 1024);
                    if (i > 0)
                    {
                        byte[] data = new byte[i];
                        Array.Copy(buffer, data, i);
                        bytes.AddRange(data);
                    }
                }while (i > 0);

                context.Response.OutputStream.Write(bytes.ToArray(), 0, bytes.Count);

                stream.Close();
                stream.Dispose();

                //context.Response.AddHeader(WopiHeaders.ItemVersion, storage.GetFileVersion(requestData.FullPath));
                ReturnSuccess(context.Response);
            }
            catch (UnauthorizedAccessException)
            {
                ReturnFileUnknown(context.Response);
            }
            catch (FileNotFoundException)
            {
                ReturnFileUnknown(context.Response);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Processes a Unlock request
        /// </summary>
        /// <remarks>
        /// For full documentation on Unlock, see
        /// https://wopi.readthedocs.io/projects/wopirest/en/latest/files/Unlock.html
        /// </remarks>
        private void HandleUnlockRequest(HttpContext context, WopiRequest requestData)
        {
            if (!ValidateAccess(requestData, writeAccessRequired: true))
            {
                ReturnInvalidToken(context.Response);
                return;
            }

            IFileStorage storage = FileStorageFactory.CreateFileStorage();
            long         size    = storage.GetFileSize(requestData.Id);

            if (size == -1)
            {
                ReturnFileUnknown(context.Response);
                return;
            }

            string newLock = context.Request.Headers[WopiHeaders.Lock];

            lock (Locks)
            {
                LockInfo existingLock;
                if (TryGetLock(requestData.Id, out existingLock))
                {
                    if (existingLock.Lock == newLock)
                    {
                        // There is a valid lock on the file and the existing lock matches the provided one

                        // Remove the current lock
                        Locks.Remove(requestData.Id);
                        context.Response.AddHeader(WopiHeaders.ItemVersion, storage.GetFileVersion(requestData.FullPath));
                        ReturnSuccess(context.Response);
                    }
                    else
                    {
                        // The existing lock doesn't match the requested one.  Return a lock mismatch error
                        // along with the current lock
                        ReturnLockMismatch(context.Response, existingLock.Lock);
                    }
                }
                else
                {
                    // The requested lock does not exist.  That's also a lock mismatch error.
                    ReturnLockMismatch(context.Response, reason: "File not locked");
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Get the file names with link on it
        /// </summary>
        /// <returns>The file names with link</returns>
        public List <FileLink> GetFiles()
        {
            List <FileLink> files = new List <FileLink>();

            IFileStorage  storage   = FileStorageFactory.CreateFileStorage();
            List <string> fileNames = storage.GetFileNames();

            foreach (string fileName in fileNames)
            {
                string   ext      = string.Empty;
                FileLink fileLink = new FileLink();
                fileLink.Name = fileName;
                if (Path.HasExtension(fileName))
                {
                    ext = Path.GetExtension(fileName);
                }

                // WOPI Validator now requires the wopi test file to have a filename, "." and the "wopitest" extenstion.
                if ((!string.IsNullOrEmpty(ext)) && (ext == ".wopitest"))
                {
                    fileLink.Url = string.Format("http://{0}/WopiValidator/Index/{1}",
                                                 ConfigurationManager.AppSettings["WOPIServerName"],
                                                 fileName);
                }

                else
                {
                    fileLink.Url = string.Format("http://{0}/wopiframe/Index/{1}",
                                                 ConfigurationManager.AppSettings["WOPIServerName"],
                                                 fileName);
                }

                files.Add(fileLink);
            }

            return(files);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Processes a PutFile request
        /// </summary>
        /// <remarks>
        /// For full documentation on PutFile, see
        /// https://wopi.readthedocs.io/projects/wopirest/en/latest/files/PutFile.html
        /// </remarks>
        private void HandlePutFileRequest(HttpContext context, WopiRequest requestData)
        {
            if (!ValidateAccess(requestData, writeAccessRequired: true))
            {
                ReturnInvalidToken(context.Response);
                return;
            }

            IFileStorage storage = FileStorageFactory.CreateFileStorage();
            long         size    = storage.GetFileSize(requestData.Id);

            if (size == -1)
            {
                ReturnFileUnknown(context.Response);
                return;
            }

            string   newLock = context.Request.Headers[WopiHeaders.Lock];
            LockInfo existingLock;
            bool     hasExistingLock;

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

            if (hasExistingLock && existingLock.Lock != newLock)
            {
                // lock mismatch/locked by another interface
                ReturnLockMismatch(context.Response, existingLock.Lock);
                return;
            }

            // 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 && size != 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.
            try
            {
                // TODO: Should be replaced with proper file save logic to a real storage system and ensures write atomicity
                int result = storage.UploadFile(requestData.Id, context.Request.InputStream);
                if (result != 0)
                {
                    ReturnServerError(context.Response);
                    return;
                }

                context.Response.AddHeader(WopiHeaders.ItemVersion, storage.GetFileVersion(requestData.FullPath));

                ReturnSuccess(context.Response);
            }
            catch (UnauthorizedAccessException)
            {
                ReturnFileUnknown(context.Response);
            }
            catch (IOException)
            {
                ReturnServerError(context.Response);
            }
        }