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)); } }
private IEnumerable <VfsStatEntry> GetDirectoryResponse(FileSystemInfoBase[] infos) { string baseAddress = Request.RequestUri.AbsoluteUri.Split('?').First(); string query = Request.RequestUri.Query; foreach (FileSystemInfoBase fileSysInfo in infos) { bool isDirectory = (fileSysInfo.Attributes & FileAttributes.Directory) != 0; string mime = isDirectory ? _directoryMediaType.ToString() : MediaTypeMap.GetMediaType(fileSysInfo.Extension).ToString(); string unescapedHref = isDirectory ? fileSysInfo.Name + UriSegmentSeparator : fileSysInfo.Name; long size = isDirectory ? 0 : ((FileInfoBase)fileSysInfo).Length; yield return(new VfsStatEntry { Name = fileSysInfo.Name, MTime = fileSysInfo.LastWriteTimeUtc, CRTime = fileSysInfo.CreationTimeUtc, Mime = mime, Size = size, Href = (baseAddress + Uri.EscapeUriString(unescapedHref) + query).EscapeHashCharacter(), Path = fileSysInfo.FullName }); } // add special folders when requesting Root url IHttpRouteData routeData = Request.GetRouteData(); if (routeData != null && String.IsNullOrEmpty(routeData.Values["path"] as string)) { foreach (var entry in VfsSpecialFolders.GetEntries(baseAddress, query)) { yield return(entry); } } }
private string GetOriginalLocalFilePath() { IHttpRouteData routeData = Request.GetRouteData(); string result; if (VfsSpecialFolders.TryParse(routeData, out result)) { return(result); } result = RootPath; if (routeData != null) { string path = routeData.Values["path"] as string; if (!String.IsNullOrEmpty(path)) { result = FileSystemHelpers.GetFullPath(Path.Combine(result, path)); } else { string reqUri = Request.RequestUri.AbsoluteUri.Split('?').First(); if (reqUri[reqUri.Length - 1] == UriSegmentSeparator) { result = Path.GetFullPath(result + Path.DirectorySeparatorChar); } } } return(result); }
private string GetOriginalLocalFilePath() { // CORE TODO No longer Request.GetRouteData(), just RouteData property on controller. // Make sure everything still works. string result; if (VfsSpecialFolders.TryParse(RouteData, out result)) { return(result); } result = RootPath; if (RouteData != null) { string path = RouteData.Values["path"] as string; if (!String.IsNullOrEmpty(path)) { result = FileSystemHelpers.GetFullPath(Path.Combine(result, path)); } else { string reqUri = UriHelper.GetRequestUri(Request).AbsoluteUri.Split('?').First(); if (reqUri[reqUri.Length - 1] == UriSegmentSeparator) { result = Path.GetFullPath(result + Path.DirectorySeparatorChar); } } } return(result); }
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)); } }
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)); } }