Browse files

Use TemporaryFile for downloaded files.

  • Loading branch information...
1 parent 0dd114f commit 71dfe646930f9f713d34cfafabcf675d1b7165f9 @spraints spraints committed May 21, 2012
View
5 GitTfs.Vs11/TfsHelper.Vs11.cs
@@ -152,17 +152,18 @@ public ItemDownloadStrategy(TfsApiBridge bridge)
_bridge = bridge;
}
- public string DownloadFile(IItem item)
+ public TemporaryFile DownloadFile(IItem item)
{
+ var temp = new TemporaryFile();
try
{
- var temp = Path.GetTempFileName();
_bridge.Unwrap<Item>(item).DownloadFile(temp);
return temp;
}
catch (Exception e)
{
Trace.WriteLine(String.Format("Something went wrong downloading \"{0}\" in changeset {1}", item.ServerItem, item.ChangesetId));
+ temp.Dispose();
throw;
}
}
View
4 GitTfs.Vs2008/TfsHelper.Vs2008.cs
@@ -70,9 +70,9 @@ public ItemDownloadStrategy(TfsApiBridge bridge)
_bridge = bridge;
}
- public string DownloadFile(IItem item)
+ public TemporaryFile DownloadFile(IItem item)
{
- string tempfile = Path.GetTempFileName();
+ var tempfile = new TemporaryFile();
_bridge.Unwrap<Item>(item).DownloadFile(tempfile);
return tempfile;
}
View
7 GitTfs.Vs2010/TfsHelper.Vs2010.cs
@@ -151,17 +151,18 @@ public ItemDownloadStrategy(TfsApiBridge bridge)
_bridge = bridge;
}
- public string DownloadFile(IItem item)
+ public TemporaryFile DownloadFile(IItem item)
{
+ var temp = new TemporaryFile();
try
{
- var temp = Path.GetTempFileName();
- _bridge.Unwrap<Item>(item).DownloadFile(temp);
+ _bridge.Unwrap<Item>(item).DownloadFile(temp);
return temp;
}
catch (Exception e)
{
Trace.WriteLine(String.Format("Something went wrong downloading \"{0}\" in changeset {1}", item.ServerItem, item.ChangesetId));
+ temp.Dispose();
throw;
}
}
View
4 GitTfs.VsCommon/TfsHelper.Common.cs
@@ -415,9 +415,9 @@ public long ContentLength
}
}
- public string DownloadFile()
+ public TemporaryFile DownloadFile()
{
- string temp = Path.GetTempFileName();
+ var temp = new TemporaryFile();
_pendingChange.DownloadShelvedFile(temp);
_contentLength = new FileInfo(temp).Length;
return temp;
View
3 GitTfs.VsCommon/Wrappers.cs
@@ -5,6 +5,7 @@
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.VersionControl.Client;
using Sep.Git.Tfs.Core.TfsInterop;
+using Sep.Git.Tfs.Util;
namespace Sep.Git.Tfs.VsCommon
{
@@ -175,7 +176,7 @@ public long ContentLength
get { return _item.ContentLength; }
}
- public string DownloadFile()
+ public TemporaryFile DownloadFile()
{
return _downloadStrategy.DownloadFile(this);
}
View
18 GitTfs.VsFake/TfsHelper.VsFake.cs
@@ -5,6 +5,7 @@
using Sep.Git.Tfs.Commands;
using Sep.Git.Tfs.Core;
using Sep.Git.Tfs.Core.TfsInterop;
+using Sep.Git.Tfs.Util;
using StructureMap;
namespace Sep.Git.Tfs.VsFake
@@ -155,16 +156,19 @@ int IItem.ItemId
long IItem.ContentLength
{
- get { return ((IItem)this).DownloadFile().Length; }
+ get
+ {
+ using (var temp = ((IItem)this).DownloadFile())
+ return new FileInfo(temp).Length;
+ }
}
- string IItem.DownloadFile()
+ TemporaryFile IItem.DownloadFile()
{
- var written = new FileStream(Path.GetTempFileName(),FileMode.Open);
- var writer = new StreamWriter(written);
- writer.Write(_change.Content);
- writer.Flush();
- return written.Name;
+ var temp = new TemporaryFile();
+ using(var writer = new StreamWriter(temp))
+ writer.Write(_change.Content);
+ return temp;
}
}
View
6 GitTfs/Core/TfsInterop/IItem.cs
@@ -1,4 +1,6 @@
using System.IO;
+using Sep.Git.Tfs.Util;
+
namespace Sep.Git.Tfs.Core.TfsInterop
{
public interface IItem
@@ -10,11 +12,11 @@ public interface IItem
TfsItemType ItemType { get; }
int ItemId { get; }
long ContentLength { get; }
- string DownloadFile();
+ TemporaryFile DownloadFile();
}
public interface IItemDownloadStrategy
{
- string DownloadFile(IItem item);
+ TemporaryFile DownloadFile(IItem item);
}
}
View
1 GitTfs/GitTfs.csproj
@@ -171,6 +171,7 @@
<Compile Include="Util\ExceptionFormattingExtensions.cs" />
<Compile Include="Util\GitTfsCommandFactory.cs" />
<Compile Include="Util\GitTfsCommandRunner.cs" />
+ <Compile Include="Util\TemporaryFile.cs" />
<Compile Include="Util\WhenDynamicDoesntWork.cs" />
<Compile Include="Util\PluggableWithAliases.cs" />
<Compile Include="Util\ConfiguresStructureMap.cs" />
View
39 GitTfs/Util/TemporaryFile.cs
@@ -0,0 +1,39 @@
+using System;
+using System.Diagnostics;
+using System.IO;
+
+namespace Sep.Git.Tfs.Util
+{
+ public class TemporaryFile : IDisposable
+ {
+ public TemporaryFile() : this(System.IO.Path.GetTempFileName())
+ {
+ }
+
+ public TemporaryFile(string path)
+ {
+ Path = path;
+ }
+
+ public string Path { get; private set; }
+
+ public static implicit operator string(TemporaryFile f)
+ {
+ return f.Path;
+ }
+
+ public void Dispose()
+ {
+ try
+ {
+ if (Path != null && File.Exists(Path))
+ File.Delete(Path);
+ Path = null;
+ }
+ catch (Exception e)
+ {
+ Trace.WriteLine("[TemporaryFile] Unable to remove " + Path + ": " + e);
+ }
+ }
+ }
+}

0 comments on commit 71dfe64

Please sign in to comment.