Beispiel #1
0
        private string GetOriginalLocalFilePath(HttpRequest request)
        {
            string     result = null;
            PathString path   = null;

            if (request.Path.StartsWithSegments("/admin/vfs", out path) ||
                request.Path.StartsWithSegments("/admin/zip", out path))
            {
                if (VfsSpecialFolders.TryParse(path, out result))
                {
                    return(result);
                }
            }

            result = RootPath;
            if (path != null && path.HasValue)
            {
                result = Path.GetFullPath(Path.Combine(result, path.Value.TrimStart('/')));
            }
            else
            {
                string reqUri = request.GetRequestUri().AbsoluteUri.Split('?').First();
                if (reqUri[reqUri.Length - 1] == UriSegmentSeparator)
                {
                    result = Path.GetFullPath(result + Path.DirectorySeparatorChar);
                }
            }
            return(result);
        }
Beispiel #2
0
        public virtual Task <HttpResponseMessage> PutItem(HttpRequest request)
        {
            var localFilePath = GetLocalFilePath(request);

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

            var info       = FileUtility.DirectoryInfoFromDirectoryName(localFilePath);
            var itemExists = info.Attributes >= 0;

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

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

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

            var dirInfo = FileUtility.DirectoryInfoFromDirectoryName(localFilePath);

            if (dirInfo.Attributes < 0)
            {
                var notFoundResponse = CreateResponse(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)
                {
                    _logger.LogError(ex, ex.Message);
                    var conflictDirectoryResponse = CreateResponse(HttpStatusCode.Conflict, ex);
                    return(Task.FromResult(conflictDirectoryResponse));
                }

                // Delete directory succeeded.
                var successResponse = 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)
                {
                    var redirectResponse = CreateResponse(HttpStatusCode.TemporaryRedirect);
                    var location         = new UriBuilder(request.GetRequestUri());
                    location.Path = location.Path.TrimEnd(_uriSegmentSeparator);
                    redirectResponse.Headers.Location = location.Uri;
                    return(Task.FromResult(redirectResponse));
                }

                // We are ready to delete the file
                var fileInfo = FileUtility.FileInfoFromFileName(localFilePath);
                return(CreateFileDeleteResponse(request, fileInfo));
            }
        }
Beispiel #4
0
        public virtual Task <HttpResponseMessage> GetItem(HttpRequest request)
        {
            string localFilePath = GetLocalFilePath(request);

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

            var info = FileUtility.DirectoryInfoFromDirectoryName(localFilePath);

            if (info.Attributes < 0)
            {
                var notFoundResponse = CreateResponse(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
                var uri = request.GetRequestUri();
                if (!uri.AbsolutePath.EndsWith("/"))
                {
                    var redirectResponse = CreateResponse(HttpStatusCode.TemporaryRedirect);
                    var location         = new UriBuilder(uri);
                    location.Path += "/";
                    redirectResponse.Headers.Location = location.Uri;
                    return(Task.FromResult(redirectResponse));
                }
                else
                {
                    return(CreateDirectoryGetResponse(request, 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 = CreateResponse(HttpStatusCode.TemporaryRedirect);
                    UriBuilder          location         = new UriBuilder(request.GetRequestUri());
                    location.Path = location.Path.TrimEnd(_uriSegmentSeparator);
                    redirectResponse.Headers.Location = location.Uri;
                    return(Task.FromResult(redirectResponse));
                }

                // We are ready to get the file
                return(CreateItemGetResponse(request, info, localFilePath));
            }
        }
Beispiel #5
0
        private string GetOriginalLocalFilePath(HttpRequest request)
        {
            string     result = null;
            PathString path   = null;

            if (request.Path.StartsWithSegments("/admin/vfs", out path) ||
                request.Path.StartsWithSegments("/admin/zip", out path))
            {
                if (VfsSpecialFolders.TryParse(path, out result))
                {
                    return(result);
                }
            }

            if (request.Query.TryGetValue("relativePath", out StringValues values) &&
                values.Contains("1"))
            {
                // the VFS path should be treated as relative to the functions script root directory
                result = Path.GetFullPath(_options.CurrentValue.ScriptPath);
            }
            else
            {
                result = RootPath;
            }

            if (path != null && path.HasValue)
            {
                result = Path.GetFullPath(Path.Combine(result, path.Value.TrimStart('/')));
            }
            else
            {
                string reqUri = request.GetRequestUri().AbsoluteUri.Split('?').First();
                if (reqUri[reqUri.Length - 1] == UriSegmentSeparator)
                {
                    result = Path.GetFullPath(result + Path.DirectorySeparatorChar);
                }
            }
            return(result);
        }