Exemplo n.º 1
0
 private void SmartDeleteFile(FileInfoBase fileToDelete)
 {
     TryFileFuncAndMoveFileOnFailure(() =>
     {
         fileToDelete.Delete();
         return(null);
     }, fileToDelete.FullName);
 }
Exemplo n.º 2
0
 protected virtual Task <IActionResult> CreateFileDeleteResponse(FileInfoBase info)
 {
     // Generate file response
     try
     {
         using (Stream fileStream = GetFileDeleteStream(info))
         {
             info.Delete();
         }
         return(Task.FromResult((IActionResult)Ok()));
     }
     catch (Exception e)
     {
         // Could not delete the file
         Tracer.TraceError(e);
         return(Task.FromResult((IActionResult)NotFound(e)));
     }
 }
Exemplo n.º 3
0
 protected virtual Task <HttpResponseMessage> CreateFileDeleteResponse(FileInfoBase info)
 {
     // Generate file response
     try
     {
         using (Stream fileStream = GetFileDeleteStream(info))
         {
             info.Delete();
         }
         HttpResponseMessage successResponse = Request.CreateResponse(HttpStatusCode.OK);
         return(Task.FromResult(successResponse));
     }
     catch (Exception e)
     {
         // Could not delete the file
         Tracer.TraceError(e);
         HttpResponseMessage notFoundResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, e);
         return(Task.FromResult(notFoundResponse));
     }
 }
Exemplo n.º 4
0
        protected Task <HttpResponseMessage> CreateFileDeleteResponse(HttpRequest request, FileInfoBase info)
        {
            // Existing resources require an etag to be updated.
            var typedHeaders = request.GetTypedHeaders();

            if (typedHeaders.IfMatch == null)
            {
                var conflictDirectoryResponse = CreateResponse(HttpStatusCode.PreconditionFailed, "Missing If-Match");
                return(Task.FromResult(conflictDirectoryResponse));
            }

            // Get current etag
            var currentEtag = CreateEntityTag(info);
            var isMatch     = typedHeaders.IfMatch.Any(etag => etag == Microsoft.Net.Http.Headers.EntityTagHeaderValue.Any || currentEtag.Equals(etag));

            if (!isMatch)
            {
                var conflictFileResponse = CreateResponse(HttpStatusCode.PreconditionFailed, "Etag mismatch");
                conflictFileResponse.Headers.ETag = currentEtag.ToSystemETag();
                return(Task.FromResult(conflictFileResponse));
            }

            try
            {
                using (Stream fileStream = GetFileDeleteStream(info))
                {
                    info.Delete();
                }
                var successResponse = CreateResponse(HttpStatusCode.OK);
                return(Task.FromResult(successResponse));
            }
            catch (Exception ex)
            {
                // Could not delete the file
                _logger.LogError(ex, ex.Message);
                var notFoundResponse = CreateResponse(HttpStatusCode.NotFound, ex);
                return(Task.FromResult(notFoundResponse));
            }
        }
Exemplo n.º 5
0
 public ActionResult IndexDelete([FromPath] FileInfoBase path)
 {
     path.Delete();
     return(new EmptyResult());
 }
Exemplo n.º 6
0
        public static IDictionary <string, string> Extract(this ZipArchive archive, string directoryName, bool preserveSymlinks = false)
        {
            IDictionary <string, string> symLinks = new Dictionary <string, string>();
            bool isSymLink = false;

            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                string path = Path.Combine(directoryName, entry.FullName);
                if (entry.Length == 0 && (path.EndsWith("/", StringComparison.Ordinal) || path.EndsWith("\\", StringComparison.Ordinal)))
                {
                    // Extract directory
                    FileSystemHelpers.CreateDirectory(path);
                }
                else
                {
                    FileInfoBase fileInfo = FileSystemHelpers.FileInfoFromFileName(path);

                    if (!fileInfo.Directory.Exists)
                    {
                        fileInfo.Directory.Create();
                    }
                    using (Stream zipStream = entry.Open(),
                           fileStream = fileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
                    {
                        zipStream.CopyTo(fileStream);
                    }

                    isSymLink = false;
                    string originalFileName = string.Empty;

                    if (!OSDetector.IsOnWindows())
                    {
                        try
                        {
                            using (Stream fs = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                            {
                                byte[] buffer = new byte[4];
                                fs.Read(buffer, 0, buffer.Length);
                                fs.Close();

                                var str = System.Text.Encoding.Default.GetString(buffer);
                                if (preserveSymlinks && str.StartsWith("../"))
                                {
                                    using (StreamReader reader = fileInfo.OpenText())
                                    {
                                        symLinks[entry.FullName] = reader.ReadToEnd();
                                    }
                                    isSymLink = true;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Could not identify symlinks in zip file : " + ex.ToString());
                        }
                    }

                    try
                    {
                        fileInfo.LastWriteTimeUtc = entry.LastWriteTime.ToUniversalTime().DateTime;
                    }
                    catch (Exception)
                    {
                        //best effort
                    }

                    if (isSymLink)
                    {
                        fileInfo.Delete();
                    }
                }
            }
            return(symLinks);
        }
Exemplo n.º 7
0
        public static void Extract(this ZipArchive archive, string directoryName)
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                string path = Path.Combine(directoryName, entry.FullName);
                if (entry.Length == 0 && (path.EndsWith("/", StringComparison.Ordinal) || path.EndsWith("\\", StringComparison.Ordinal)))
                {
                    // Extract directory
                    FileSystemHelpers.CreateDirectory(path);
                }
                else
                {
                    FileInfoBase fileInfo = FileSystemHelpers.FileInfoFromFileName(path);

                    if (!fileInfo.Directory.Exists)
                    {
                        fileInfo.Directory.Create();
                    }

                    using (Stream zipStream = entry.Open(),
                           fileStream = fileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
                    {
                        zipStream.CopyTo(fileStream);
                    }

                    bool   createSymLink    = false;
                    string originalFileName = string.Empty;
                    if (!OSDetector.IsOnWindows())
                    {
                        try
                        {
                            using (Stream fs = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                            {
                                byte[] buffer = new byte[10];
                                fs.Read(buffer, 0, buffer.Length);
                                fs.Close();

                                var str = System.Text.Encoding.Default.GetString(buffer);
                                if (str.StartsWith("../"))
                                {
                                    string fullPath = Path.GetFullPath(str);
                                    if (fullPath.StartsWith(directoryName))
                                    {
                                        createSymLink    = true;
                                        originalFileName = fullPath;
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Could not identify symlinks in zip file : " + ex.ToString());
                        }
                    }

                    fileInfo.LastWriteTimeUtc = entry.LastWriteTime.ToUniversalTime().DateTime;

                    if (createSymLink)
                    {
                        try
                        {
                            fileInfo.Delete();

                            Mono.Unix.UnixFileInfo unixFileInfo = new Mono.Unix.UnixFileInfo(originalFileName);
                            unixFileInfo.CreateSymbolicLink(path);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Could not create symlinks : " + ex.ToString());
                        }
                    }
                }
            }
        }