Exemple #1
0
        public virtual Task <HttpResponseMessage> PutItem()
        {
            string localFilePath = GetLocalFilePath();

            HttpResponseMessage response;

            if (VfsSpecialFolders.TryHandleRequest(Request, localFilePath, out response))
            {
                return(Task.FromResult(response));
            }

            DirectoryInfoBase info = FileSystemHelpers.DirectoryInfoFromDirectoryName(localFilePath);
            bool itemExists        = info.Attributes >= 0;

            if (itemExists && (info.Attributes & FileAttributes.Directory) != 0)
            {
                return(CreateDirectoryPutResponse(info, localFilePath));
            }
            else
            {
                // If request URI ends in a "/" then attempt to create the directory.
                if (localFilePath[localFilePath.Length - 1] == Path.DirectorySeparatorChar)
                {
                    return(CreateDirectoryPutResponse(info, localFilePath));
                }

                // We are ready to update the file
                return(CreateItemPutResponse(info, localFilePath, itemExists));
            }
        }
Exemple #2
0
        public virtual Task <HttpResponseMessage> DeleteItem(bool recursive = false)
        {
            string localFilePath = GetLocalFilePath();

            HttpResponseMessage response;

            if (VfsSpecialFolders.TryHandleRequest(Request, localFilePath, out response))
            {
                return(Task.FromResult(response));
            }

            DirectoryInfoBase dirInfo = FileSystemHelpers.DirectoryInfoFromDirectoryName(localFilePath);

            if (dirInfo.Attributes < 0)
            {
                HttpResponseMessage notFoundResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, String.Format("'{0}' not found.", dirInfo.FullName));
                return(Task.FromResult(notFoundResponse));
            }
            else if ((dirInfo.Attributes & FileAttributes.Directory) != 0)
            {
                try
                {
                    dirInfo.Delete(recursive);
                }
                catch (Exception ex)
                {
                    Tracer.TraceError(ex);
                    HttpResponseMessage conflictDirectoryResponse = Request.CreateErrorResponse(
                        HttpStatusCode.Conflict, Resources.VfsControllerBase_CannotDeleteDirectory);
                    return(Task.FromResult(conflictDirectoryResponse));
                }

                // Delete directory succeeded.
                HttpResponseMessage successResponse = Request.CreateResponse(HttpStatusCode.OK);
                return(Task.FromResult(successResponse));
            }
            else
            {
                // If request URI ends in a "/" then redirect to one that does not
                if (localFilePath[localFilePath.Length - 1] == Path.DirectorySeparatorChar)
                {
                    HttpResponseMessage redirectResponse = Request.CreateResponse(HttpStatusCode.TemporaryRedirect);
                    UriBuilder          location         = new UriBuilder(Request.RequestUri);
                    location.Path = location.Path.TrimEnd(_uriSegmentSeparator);
                    redirectResponse.Headers.Location = location.Uri;
                    return(Task.FromResult(redirectResponse));
                }

                // We are ready to delete the file
                var fileInfo = FileSystemHelpers.FileInfoFromFileName(localFilePath);
                return(CreateFileDeleteResponse(fileInfo));
            }
        }
Exemple #3
0
        public virtual Task <HttpResponseMessage> GetItem()
        {
            string localFilePath = GetLocalFilePath();

            HttpResponseMessage response;

            if (VfsSpecialFolders.TryHandleRequest(Request, localFilePath, out response))
            {
                return(Task.FromResult(response));
            }

            DirectoryInfoBase info = FileSystemHelpers.DirectoryInfoFromDirectoryName(localFilePath);

            if (info.Attributes < 0)
            {
                HttpResponseMessage notFoundResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, String.Format("'{0}' not found.", info.FullName));
                return(Task.FromResult(notFoundResponse));
            }
            else if ((info.Attributes & FileAttributes.Directory) != 0)
            {
                // If request URI does NOT end in a "/" then redirect to one that does
                if (localFilePath[localFilePath.Length - 1] != Path.DirectorySeparatorChar)
                {
                    HttpResponseMessage redirectResponse = Request.CreateResponse(HttpStatusCode.TemporaryRedirect);
                    UriBuilder          location         = new UriBuilder(Request.RequestUri);
                    location.Path += "/";
                    redirectResponse.Headers.Location = location.Uri;
                    return(Task.FromResult(redirectResponse));
                }
                else
                {
                    return(CreateDirectoryGetResponse(info, localFilePath));
                }
            }
            else
            {
                // If request URI ends in a "/" then redirect to one that does not
                if (localFilePath[localFilePath.Length - 1] == Path.DirectorySeparatorChar)
                {
                    HttpResponseMessage redirectResponse = Request.CreateResponse(HttpStatusCode.TemporaryRedirect);
                    UriBuilder          location         = new UriBuilder(Request.RequestUri);
                    location.Path = location.Path.TrimEnd(_uriSegmentSeparator);
                    redirectResponse.Headers.Location = location.Uri;
                    return(Task.FromResult(redirectResponse));
                }

                // We are ready to get the file
                return(CreateItemGetResponse(info, localFilePath));
            }
        }
        public virtual Task <IActionResult> DeleteItem(bool recursive = false)
        {
            string localFilePath = GetLocalFilePath();

            if (VfsSpecialFolders.TryHandleRequest(Request, localFilePath, out IActionResult response))
            {
                return(Task.FromResult(response));
            }

            DirectoryInfoBase dirInfo = FileSystemHelpers.DirectoryInfoFromDirectoryName(localFilePath);

            if (dirInfo.Attributes < 0)
            {
                return(Task.FromResult((IActionResult)NotFound(String.Format("'{0}' not found.", dirInfo.FullName))));
            }
            else if ((dirInfo.Attributes & FileAttributes.Directory) != 0)
            {
                try
                {
                    dirInfo.Delete(recursive);
                }
                catch (Exception ex)
                {
                    Tracer.TraceError(ex);
                    return(Task.FromResult((IActionResult)StatusCode(StatusCodes.Status409Conflict, Resources.VfsControllerBase_CannotDeleteDirectory)));
                }

                // Delete directory succeeded.
                return(Task.FromResult((IActionResult)Ok()));
            }
            else
            {
                // If request URI ends in a "/" then redirect to one that does not
                if (localFilePath[localFilePath.Length - 1] == Path.DirectorySeparatorChar)
                {
                    UriBuilder location = new UriBuilder(UriHelper.GetRequestUri(Request));
                    location.Path = location.Path.TrimEnd(_uriSegmentSeparator);
                    return(Task.FromResult((IActionResult)RedirectPreserveMethod(location.Uri.ToString())));
                }

                // We are ready to delete the file
                var fileInfo = FileSystemHelpers.FileInfoFromFileName(localFilePath);
                return(CreateFileDeleteResponse(fileInfo));
            }
        }
        public virtual Task <IActionResult> GetItem()
        {
            string localFilePath = GetLocalFilePath();

            if (VfsSpecialFolders.TryHandleRequest(Request, localFilePath, out IActionResult response))
            {
                return(Task.FromResult(response));
            }

            DirectoryInfoBase info = FileSystemHelpers.DirectoryInfoFromDirectoryName(localFilePath);

            if (info.Attributes < 0)
            {
                return(Task.FromResult((IActionResult)NotFound(String.Format("'{0}' not found.", info.FullName))));
            }
            else if ((info.Attributes & FileAttributes.Directory) != 0)
            {
                // If request URI does NOT end in a "/" then redirect to one that does
                if (localFilePath[localFilePath.Length - 1] != Path.DirectorySeparatorChar)
                {
                    UriBuilder location = new UriBuilder(UriHelper.GetRequestUri(Request));
                    location.Path += "/";
                    return(Task.FromResult((IActionResult)RedirectPreserveMethod(location.Uri.ToString())));
                }
                else
                {
                    return(CreateDirectoryGetResponse(info, localFilePath));
                }
            }
            else
            {
                // If request URI ends in a "/" then redirect to one that does not
                if (localFilePath[localFilePath.Length - 1] == Path.DirectorySeparatorChar)
                {
                    UriBuilder location = new UriBuilder(UriHelper.GetRequestUri(Request));
                    location.Path = location.Path.TrimEnd(_uriSegmentSeparator);
                    return(Task.FromResult((IActionResult)RedirectPreserveMethod(location.Uri.ToString())));
                }

                // We are ready to get the file
                return(CreateItemGetResponse(info, localFilePath));
            }
        }