Skip to content

Commit

Permalink
Cleaned up TemporaryFile class. Issue fo-dicom#69
Browse files Browse the repository at this point in the history
  • Loading branch information
anders9ustafsson committed Sep 2, 2015
1 parent 57204f2 commit 586a23e
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 98 deletions.
4 changes: 2 additions & 2 deletions DICOM [Unit Tests]/IO/DesktopFileReferenceTest.cs
Expand Up @@ -17,7 +17,7 @@ public void Constructor_TempFile_TempFileAttributeSet()
var path = @".\Test Data\tmp.tmp";
File.Create(path).Dispose();

var file = new DesktopFileReference(path, true);
var file = new DesktopFileReference(path) { IsTempFile = true };
Assert.True((File.GetAttributes(path) & FileAttributes.Temporary) == FileAttributes.Temporary);
}

Expand All @@ -27,7 +27,7 @@ public void Constructor_RegularFile_TempFileAttributeNotSet()
var path = @".\Test Data\tmp.tmp";
File.Create(path).Dispose();

var file = new DesktopFileReference(path, false);
var file = new DesktopFileReference(path);
Assert.False((File.GetAttributes(path) & FileAttributes.Temporary) == FileAttributes.Temporary);
}

Expand Down
76 changes: 58 additions & 18 deletions DICOM [Unit Tests]/IO/TemporaryFileTest.cs
Expand Up @@ -9,16 +9,25 @@ namespace Dicom.IO

public class TemporaryFileTest
{
#region Fields

private readonly object locker = new object();

#endregion

#region Unit tests

[Fact]
public void StoragePath_Setter_DirectoryCreatedIfNonExisting()
{
var path = @".\Test Data\Temporary Path";
if (Directory.Exists(path)) Directory.Delete(path);
lock (this.locker)
{
var path = @".\Test Data\Temporary Path 1";
if (Directory.Exists(path)) Directory.Delete(path);

TemporaryFile.StoragePath = path;
Assert.True(Directory.Exists(path));
TemporaryFile.StoragePath = path;
Assert.True(Directory.Exists(path));
}
}

[Fact]
Expand All @@ -30,35 +39,66 @@ public void StoragePath_Setter_NullShouldNotThrow()
[Fact]
public void StoragePath_Getter_DefaultEqualToUserTemp()
{
var expected = Path.GetTempPath();
lock (this.locker)
{
var expected = Path.GetTempPath();

TemporaryFile.StoragePath = null;
var actual = TemporaryFile.StoragePath;
TemporaryFile.StoragePath = null;
var actual = TemporaryFile.StoragePath;

Assert.Equal(expected, actual);
Assert.Equal(expected, actual);
}
}

[Fact]
public void Create_StoragePathNull_LocatedInUserTemp()
{
TemporaryFile.StoragePath = null;
var temp = TemporaryFile.Create();
lock (this.locker)
{
TemporaryFile.StoragePath = null;
var temp = TemporaryFile.Create().Name;

var expected = Path.GetTempPath().TrimEnd('\\');
var actual = Path.GetDirectoryName(temp);
Assert.Equal(expected, actual);
var expected = Path.GetTempPath().TrimEnd('\\');
var actual = Path.GetDirectoryName(temp);
Assert.Equal(expected, actual);
}
}

[Fact]
public void Create_StoragePathNonNull_LocatedInSpecDirectory()
{
var expected = @".\Test Data\Temporary Directory";
TemporaryFile.StoragePath = expected;
lock (this.locker)
{
var expected = @".\Test Data\Temporary Path 2";
TemporaryFile.StoragePath = expected;

var temp = TemporaryFile.Create().Name;

var temp = TemporaryFile.Create();
var actual = Path.GetDirectoryName(temp);
Assert.Equal(expected, actual);
}
}

var actual = Path.GetDirectoryName(temp);
Assert.Equal(expected, actual);
[Fact]
public void Create_StoragePathNonNull_FileAttributesContainTempFlag()
{
lock (this.locker)
{
TemporaryFile.StoragePath = @".\Test Data\Temporary Path 3";
var path = TemporaryFile.Create().Name;
Assert.True((File.GetAttributes(path) & FileAttributes.Temporary) == FileAttributes.Temporary);
}
}

[Fact]
public void Create_StoragePathNull_FileAttributesContainTempFlag()
{
lock (this.locker)
{
TemporaryFile.StoragePath = null;
var path = TemporaryFile.Create().Name;
Assert.True((File.GetAttributes(path) & FileAttributes.Temporary) == FileAttributes.Temporary);
}
}

#endregion
Expand Down
4 changes: 1 addition & 3 deletions DICOM/IO/Buffer/TempFileBuffer.cs
Expand Up @@ -24,9 +24,7 @@ public sealed class TempFileBuffer : IByteBuffer
/// <param name="data">Byte array subject to buffering.</param>
public TempFileBuffer(byte[] data)
{
var tempFile = new TemporaryFile();

this.file = IOManager.CreateFileReference(tempFile.Name);
this.file = TemporaryFile.Create();
this.Size = (uint)data.Length;

using (var stream = this.file.OpenWrite())
Expand Down
12 changes: 5 additions & 7 deletions DICOM/IO/DesktopFileReference.cs
Expand Up @@ -16,11 +16,10 @@ public sealed class DesktopFileReference : IFileReference
/// Initializes a <see cref="DesktopFileReference"/> object.
/// </summary>
/// <param name="fileName">File name.</param>
/// <param name="isTempFile">Indicates whether the file is a temporary file or not.</param>
public DesktopFileReference(string fileName, bool isTempFile = false)
public DesktopFileReference(string fileName)
{
this.Name = fileName;
this.IsTempFile = isTempFile;
this.IsTempFile = false;
}

/// <summary>
Expand Down Expand Up @@ -51,7 +50,7 @@ public bool Exists
}

/// <summary>Gets and sets whether the file is temporary or not.</summary>
/// <remarks>File will be deleted when object is <c>Disposed</c>.</remarks>
/// <remarks>Temporary file will be deleted when object is <c>Disposed</c>.</remarks>
public bool IsTempFile
{
get
Expand Down Expand Up @@ -161,7 +160,7 @@ public byte[] GetByteRange(int offset, int count)
{
byte[] buffer = new byte[count];

using (Stream fs = this.OpenRead())
using (var fs = this.OpenRead())
{
fs.Seek(offset, SeekOrigin.Begin);
fs.Read(buffer, 0, count);
Expand All @@ -178,8 +177,7 @@ public byte[] GetByteRange(int offset, int count)
/// </returns>
public override string ToString()
{
if (this.IsTempFile) return string.Format("{0} [TEMP]", this.Name);
return this.Name;
return this.IsTempFile ? string.Format("{0} [TEMP]", this.Name) : this.Name;
}
}
}
5 changes: 2 additions & 3 deletions DICOM/IO/DesktopIOManager.cs
Expand Up @@ -53,11 +53,10 @@ public override IPath PathImpl
/// Platform-specific implementation to create a file reference.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="isTempFile">Indicates whether the file should be handled as a temporary file or not.</param>
/// <returns>A file reference object.</returns>
protected override IFileReference CreateFileReferenceImpl(string fileName, bool isTempFile = false)
protected override IFileReference CreateFileReferenceImpl(string fileName)
{
return new DesktopFileReference(fileName, isTempFile);
return new DesktopFileReference(fileName);
}

/// <summary>
Expand Down
8 changes: 3 additions & 5 deletions DICOM/IO/IOManager.cs
Expand Up @@ -61,11 +61,10 @@ public static void SetImplementation(IOManager impl)
/// Create a file reference.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="isTempFile">Indicates whether the file should be handled as a temporary file or not.</param>
/// <returns>A file reference object.</returns>
public static IFileReference CreateFileReference(string fileName, bool isTempFile = false)
public static IFileReference CreateFileReference(string fileName)
{
return implementation.CreateFileReferenceImpl(fileName, isTempFile);
return implementation.CreateFileReferenceImpl(fileName);
}

/// <summary>
Expand All @@ -82,9 +81,8 @@ public static IDirectoryReference CreateDirectoryReference(string directoryName)
/// Platform-specific implementation to create a file reference.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="isTempFile">Indicates whether the file should be handled as a temporary file or not.</param>
/// <returns>A file reference object.</returns>
protected abstract IFileReference CreateFileReferenceImpl(string fileName, bool isTempFile = false);
protected abstract IFileReference CreateFileReferenceImpl(string fileName);

/// <summary>
/// Platform-specific implementation to create a directory reference.
Expand Down
91 changes: 34 additions & 57 deletions DICOM/IO/TemporaryFile.cs
@@ -1,96 +1,73 @@
// Copyright (c) 2012-2015 fo-dicom contributors.
// Licensed under the Microsoft Public License (MS-PL).

using System;
using System.IO;

namespace Dicom.IO
{
public class TemporaryFile : IDisposable
{
private string _file;

public TemporaryFile()
{
_file = Create();
}

~TemporaryFile()
{
TemporaryFileRemover.Delete(_file);
}
using System;

public void Dispose()
{
TemporaryFileRemover.Delete(_file);
GC.SuppressFinalize(this);
}
/// <summary>
/// Support class for creating a temporary file.
/// </summary>
public static class TemporaryFile
{
#region FIELDS

public string Name
{
get
{
return _file;
}
}
private static string storagePath;

#region Static
#endregion

private static string _path = null;
#region PROPERTIES

/// <summary>
/// Gets or sets the directory location of the temporary files.
/// </summary>
public static string StoragePath
{
get
{
if (_path != null) return _path;
if (storagePath != null) return storagePath;
return IOManager.Path.GetTempDirectory();
}
set
{
_path = value;
if (_path != null)
storagePath = value;
if (storagePath != null)
{
var directory = IOManager.CreateDirectoryReference(_path);
var directory = IOManager.CreateDirectoryReference(storagePath);
if (!directory.Exists) directory.Create();
}
}
}

public static string Create()
#endregion

#region METHODS

/// <summary>
/// Creates a temporary file and returns its name.
/// </summary>
/// <returns>Name of the temporary file.</returns>
public static IFileReference Create()
{
string path = null;
IFileReference file;

if (_path != null)
if (storagePath != null)
{
// create file in user specified path
path = IOManager.Path.Combine(_path, Guid.NewGuid().ToString());
File.Create(path).Close();
var path = IOManager.Path.Combine(storagePath, Guid.NewGuid().ToString());
file = IOManager.CreateFileReference(path);
file.Create().Dispose();
}
else
{
// allow OS to create file in system temp path
path = IOManager.Path.GetTempFileName();
}

try
{
// set temporary file attribute so that the file system
// will attempt to keep all of the data in memory
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Temporary);
}
catch
{
// sometimes fails with invalid argument exception
file = IOManager.CreateFileReference(IOManager.Path.GetTempFileName());
}
file.IsTempFile = true;

return path;
return file;
}

#endregion

public override string ToString()
{
return String.Format("{0} [TEMP]", Name);
}
}
}
7 changes: 4 additions & 3 deletions DICOM/Network/DicomService.cs
Expand Up @@ -614,11 +614,12 @@ private void ProcessPDataTF(object state)
/// <returns>The stream to write the SopInstance to</returns>
protected virtual Stream CreateCStoreReceiveStream(DicomFile file)
{
var fileName = TemporaryFile.Create();
var temp = TemporaryFile.Create();

file.Save(fileName);
var dimseStream = temp.Open();
file.Save(dimseStream);
dimseStream.Seek(0, SeekOrigin.Begin);

var dimseStream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite);
_isTempFile = true;
dimseStream.Seek(0, SeekOrigin.End);
return dimseStream;
Expand Down

0 comments on commit 586a23e

Please sign in to comment.