예제 #1
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));
            }
        }
예제 #2
0
        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));
            }
        }
예제 #3
0
        private void SmartDirectoryDelete(DirectoryInfoBase directory, string rootPath, string targetSubFolder)
        {
            if (IgnorePath(directory))
            {
                return;
            }

            string previousDirectoryPath = FileSystemHelpers.GetRelativePath(rootPath, directory.FullName);

            if (!DoesPathExistsInManifest(previousDirectoryPath, targetSubFolder))
            {
                return;
            }

            var files          = FileSystemHelpers.GetFiles(directory);
            var subDirectories = FileSystemHelpers.GetDirectories(directory);

            foreach (var file in files.Values)
            {
                string previousFilePath = FileSystemHelpers.GetRelativePath(rootPath, file.FullName);
                if (DoesPathExistsInManifest(previousFilePath, targetSubFolder))
                {
                    _logger.Log("Deleting file: '{0}'", previousFilePath);
                    var inclosuresafe = file;
                    OperationManager.Attempt(() => SmartDeleteFile(inclosuresafe));
                }
            }

            foreach (var subDirectory in subDirectories.Values)
            {
                SmartDirectoryDelete(subDirectory, rootPath, targetSubFolder);
            }

            if (directory.IsEmpty())
            {
                _logger.Log("Deleting directory: '{0}'", previousDirectoryPath);
                OperationManager.Attempt(() => directory.Delete());
            }
        }