Skip to content

Commit

Permalink
Merge pull request #88 from tapmantwo/Clean_temp_files
Browse files Browse the repository at this point in the history
Cleaning up temp files
  • Loading branch information
holytshirt committed Oct 23, 2015
2 parents 15e2034 + 6d93a65 commit 0bdeac3
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 32 deletions.
23 changes: 22 additions & 1 deletion src/OpenRasta/IO/IFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace OpenRasta.IO
{
public interface IFile
public interface IFile : IDisposable
{
MediaType ContentType { get; }
string FileName { get; }
Expand Down Expand Up @@ -51,6 +51,7 @@ public InMemoryFile(Stream stream)
}

readonly Stream _stream;
bool _disposed;
public MediaType ContentType { get; set; }
public string FileName { get; set; }
public long Length { get; set; }
Expand All @@ -64,6 +65,26 @@ string IReceivedFile.OriginalName
{
get { return FileName; }
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (_disposed) return;

if (disposing)
{
if (_stream != null)
{
_stream.Dispose();
}
}
_disposed = true;
}
}
#pragma warning restore 0618
public class InMemoryDownloadableFile : InMemoryFile, IDownloadableFile
Expand Down
10 changes: 10 additions & 0 deletions src/OpenRasta/Pipeline/PipelineRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ protected virtual PipelineContinuation ExecuteContributor(ICommunicationContext

protected virtual void FinishPipeline(ICommunicationContext context)
{
if (context.Request.Entity != null)
{
context.Request.Entity.Dispose();
}

if (context.Response.Entity != null)
{
context.Response.Entity.Dispose();
}

PipelineLog.WriteInfo("Pipeline finished.");
}

Expand Down
22 changes: 22 additions & 0 deletions src/OpenRasta/Web/HttpEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ public MediaType ContentType

public Stream Stream { get; private set; }
public IList<Error> Errors { get; set; }

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private bool _disposed;

protected virtual void Dispose(bool disposing)
{
if (_disposed) return;

if (disposing)
{
if (Stream != null)
{
Stream.Dispose();
}
}
_disposed = true;
}
}
}

Expand Down
26 changes: 25 additions & 1 deletion src/OpenRasta/Web/HttpEntityFile.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.IO;
using OpenRasta.IO;

namespace OpenRasta.Web
{
public class HttpEntityFile : IFile
{
readonly IHttpEntity _entity;
private bool _disposed;

private IHttpEntity _entity;

public HttpEntityFile(IHttpEntity entity)
{
Expand All @@ -31,5 +34,26 @@ public Stream OpenStream()
{
return _entity.Stream;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (_disposed) return;

if (disposing)
{
if (_entity != null)
{
_entity.Dispose();
_entity = null;
}
}
_disposed = true;
}
}
}
2 changes: 1 addition & 1 deletion src/OpenRasta/Web/IHttpEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace OpenRasta.Web
{
public interface IHttpEntity
public interface IHttpEntity : IDisposable
{
ICodec Codec { get; set; }
object Instance { get; set; }
Expand Down
65 changes: 36 additions & 29 deletions src/OpenRasta/Web/MultipartHttpEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using OpenRasta.Codecs;
using OpenRasta.Diagnostics;
Expand All @@ -22,7 +21,7 @@ public interface IMultipartHttpEntity : IHttpEntity
void SwapStream(Stream stream);
}

public class MultipartHttpEntity : IMultipartHttpEntity, IDisposable
public class MultipartHttpEntity : IMultipartHttpEntity
{
ILogger Log { get; set; }

Expand Down Expand Up @@ -54,6 +53,7 @@ public Stream Stream
}
set
{
CleanUpFile();
_stream = value;
_filePath = null;
}
Expand All @@ -67,10 +67,12 @@ public Stream Stream
private string _filePath = null;
public void SwapStream(Stream stream)
{
CleanUpFile();
Stream = stream;
}
public void SwapStream(string filepath)
{
CleanUpFile();
_filePath = filepath;
_stream = null;
}
Expand All @@ -80,45 +82,50 @@ public void Dispose()
GC.SuppressFinalize(this);
}
private bool _disposed = false;

protected virtual void Dispose(bool disposing)
{
if (!_disposed)
if (_disposed) return;

if (disposing)
{
if (disposing)
if (_stream != null)
{
if (_stream != null)
try
{
try
{
_stream.Dispose();
}
catch (ObjectDisposedException) { }
finally
{
_stream = null;
}
_stream.Dispose();
}
if (_filePath != null && File.Exists(_filePath))
finally
{
try
{
File.Delete(_filePath);
}
catch (Exception e)
{
Log.Safe().WriteError("Could not delete file {0} after use. See exception for details.", _filePath);
Log.Safe().WriteException(e);
}
finally
{
_filePath = null;
}
_stream = null;
}
}

_disposed = true;
CleanUpFile();
}
_disposed = true;
}

private void CleanUpFile()
{
if (_filePath == null || !File.Exists(_filePath)) return;

try
{
File.Delete(_filePath);
}
catch (Exception e)
{
Log.Safe()
.WriteError("Could not delete file {0} after use. See exception for details.", _filePath);
Log.Safe().WriteException(e);
}
finally
{
_filePath = null;
}
}

~MultipartHttpEntity()
{
Dispose(false);
Expand Down

0 comments on commit 0bdeac3

Please sign in to comment.