Skip to content

Commit

Permalink
Use TemporaryFile for downloaded files.
Browse files Browse the repository at this point in the history
  • Loading branch information
spraints committed May 21, 2012
1 parent 0dd114f commit 71dfe64
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 19 deletions.
5 changes: 3 additions & 2 deletions GitTfs.Vs11/TfsHelper.Vs11.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -152,17 +152,18 @@ public ItemDownloadStrategy(TfsApiBridge bridge)
_bridge = bridge; _bridge = bridge;
} }


public string DownloadFile(IItem item) public TemporaryFile DownloadFile(IItem item)
{ {
var temp = new TemporaryFile();
try try
{ {
var temp = Path.GetTempFileName();
_bridge.Unwrap<Item>(item).DownloadFile(temp); _bridge.Unwrap<Item>(item).DownloadFile(temp);
return temp; return temp;
} }
catch (Exception e) catch (Exception e)
{ {
Trace.WriteLine(String.Format("Something went wrong downloading \"{0}\" in changeset {1}", item.ServerItem, item.ChangesetId)); Trace.WriteLine(String.Format("Something went wrong downloading \"{0}\" in changeset {1}", item.ServerItem, item.ChangesetId));
temp.Dispose();
throw; throw;
} }
} }
Expand Down
4 changes: 2 additions & 2 deletions GitTfs.Vs2008/TfsHelper.Vs2008.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public ItemDownloadStrategy(TfsApiBridge bridge)
_bridge = 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); _bridge.Unwrap<Item>(item).DownloadFile(tempfile);
return tempfile; return tempfile;
} }
Expand Down
7 changes: 4 additions & 3 deletions GitTfs.Vs2010/TfsHelper.Vs2010.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -151,17 +151,18 @@ public ItemDownloadStrategy(TfsApiBridge bridge)
_bridge = bridge; _bridge = bridge;
} }


public string DownloadFile(IItem item) public TemporaryFile DownloadFile(IItem item)
{ {
var temp = new TemporaryFile();
try try
{ {
var temp = Path.GetTempFileName(); _bridge.Unwrap<Item>(item).DownloadFile(temp);
_bridge.Unwrap<Item>(item).DownloadFile(temp);
return temp; return temp;
} }
catch (Exception e) catch (Exception e)
{ {
Trace.WriteLine(String.Format("Something went wrong downloading \"{0}\" in changeset {1}", item.ServerItem, item.ChangesetId)); Trace.WriteLine(String.Format("Something went wrong downloading \"{0}\" in changeset {1}", item.ServerItem, item.ChangesetId));
temp.Dispose();
throw; throw;
} }
} }
Expand Down
4 changes: 2 additions & 2 deletions GitTfs.VsCommon/TfsHelper.Common.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -415,9 +415,9 @@ public long ContentLength
} }
} }


public string DownloadFile() public TemporaryFile DownloadFile()
{ {
string temp = Path.GetTempFileName(); var temp = new TemporaryFile();
_pendingChange.DownloadShelvedFile(temp); _pendingChange.DownloadShelvedFile(temp);
_contentLength = new FileInfo(temp).Length; _contentLength = new FileInfo(temp).Length;
return temp; return temp;
Expand Down
3 changes: 2 additions & 1 deletion GitTfs.VsCommon/Wrappers.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.TeamFoundation.Server; using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.VersionControl.Client; using Microsoft.TeamFoundation.VersionControl.Client;
using Sep.Git.Tfs.Core.TfsInterop; using Sep.Git.Tfs.Core.TfsInterop;
using Sep.Git.Tfs.Util;


namespace Sep.Git.Tfs.VsCommon namespace Sep.Git.Tfs.VsCommon
{ {
Expand Down Expand Up @@ -175,7 +176,7 @@ public long ContentLength
get { return _item.ContentLength; } get { return _item.ContentLength; }
} }


public string DownloadFile() public TemporaryFile DownloadFile()
{ {
return _downloadStrategy.DownloadFile(this); return _downloadStrategy.DownloadFile(this);
} }
Expand Down
18 changes: 11 additions & 7 deletions GitTfs.VsFake/TfsHelper.VsFake.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Sep.Git.Tfs.Commands; using Sep.Git.Tfs.Commands;
using Sep.Git.Tfs.Core; using Sep.Git.Tfs.Core;
using Sep.Git.Tfs.Core.TfsInterop; using Sep.Git.Tfs.Core.TfsInterop;
using Sep.Git.Tfs.Util;
using StructureMap; using StructureMap;


namespace Sep.Git.Tfs.VsFake namespace Sep.Git.Tfs.VsFake
Expand Down Expand Up @@ -155,16 +156,19 @@ int IItem.ItemId


long IItem.ContentLength 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 temp = new TemporaryFile();
var writer = new StreamWriter(written); using(var writer = new StreamWriter(temp))
writer.Write(_change.Content); writer.Write(_change.Content);
writer.Flush(); return temp;
return written.Name;
} }
} }


Expand Down
6 changes: 4 additions & 2 deletions GitTfs/Core/TfsInterop/IItem.cs
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.IO; using System.IO;
using Sep.Git.Tfs.Util;

namespace Sep.Git.Tfs.Core.TfsInterop namespace Sep.Git.Tfs.Core.TfsInterop
{ {
public interface IItem public interface IItem
Expand All @@ -10,11 +12,11 @@ public interface IItem
TfsItemType ItemType { get; } TfsItemType ItemType { get; }
int ItemId { get; } int ItemId { get; }
long ContentLength { get; } long ContentLength { get; }
string DownloadFile(); TemporaryFile DownloadFile();
} }


public interface IItemDownloadStrategy public interface IItemDownloadStrategy
{ {
string DownloadFile(IItem item); TemporaryFile DownloadFile(IItem item);
} }
} }
1 change: 1 addition & 0 deletions GitTfs/GitTfs.csproj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
<Compile Include="Util\ExceptionFormattingExtensions.cs" /> <Compile Include="Util\ExceptionFormattingExtensions.cs" />
<Compile Include="Util\GitTfsCommandFactory.cs" /> <Compile Include="Util\GitTfsCommandFactory.cs" />
<Compile Include="Util\GitTfsCommandRunner.cs" /> <Compile Include="Util\GitTfsCommandRunner.cs" />
<Compile Include="Util\TemporaryFile.cs" />
<Compile Include="Util\WhenDynamicDoesntWork.cs" /> <Compile Include="Util\WhenDynamicDoesntWork.cs" />
<Compile Include="Util\PluggableWithAliases.cs" /> <Compile Include="Util\PluggableWithAliases.cs" />
<Compile Include="Util\ConfiguresStructureMap.cs" /> <Compile Include="Util\ConfiguresStructureMap.cs" />
Expand Down
39 changes: 39 additions & 0 deletions GitTfs/Util/TemporaryFile.cs
Original file line number Original file line Diff line number Diff line change
@@ -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.