refactor: move GetStream and URI methods from ArchiveReadRequest -> ArchiveHelpers

soprefactor
Justin Clark-Casey (justincc) 2010-05-28 19:21:00 +01:00
parent 6b568af565
commit d72435693b
3 changed files with 65 additions and 64 deletions

View File

@ -37,7 +37,6 @@ using OpenSim.Framework;
using OpenSim.Framework.Serialization; using OpenSim.Framework.Serialization;
using OpenSim.Framework.Serialization.External; using OpenSim.Framework.Serialization.External;
using OpenSim.Framework.Communications; using OpenSim.Framework.Communications;
using OpenSim.Framework.Communications.Osp; using OpenSim.Framework.Communications.Osp;
using OpenSim.Region.CoreModules.World.Archiver; using OpenSim.Region.CoreModules.World.Archiver;
using OpenSim.Region.Framework.Scenes; using OpenSim.Region.Framework.Scenes;

View File

@ -25,6 +25,9 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
using System;
using System.IO;
using System.Net;
using OpenMetaverse; using OpenMetaverse;
using OpenSim.Framework.Serialization; using OpenSim.Framework.Serialization;
using OpenSim.Region.Framework.Scenes; using OpenSim.Region.Framework.Scenes;
@ -60,5 +63,66 @@ namespace OpenSim.Region.CoreModules.World.Archiver
{ {
return ArchiveConstants.CreateOarObjectPath(sog.Name, sog.UUID, sog.AbsolutePosition); return ArchiveConstants.CreateOarObjectPath(sog.Name, sog.UUID, sog.AbsolutePosition);
} }
/// <summary>
/// Resolve path to a working FileStream
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static Stream GetStream(string path)
{
if (File.Exists(path))
{
return new FileStream(path, FileMode.Open, FileAccess.Read);
}
else
{
try
{
Uri uri = new Uri(path);
if (uri.Scheme == "file")
{
return new FileStream(uri.AbsolutePath, FileMode.Open, FileAccess.Read);
}
else
{
if (uri.Scheme != "http")
throw new Exception(String.Format("Unsupported URI scheme ({0})", path));
// OK, now we know we have an HTTP URI to work with
return URIFetch(uri);
}
}
catch (UriFormatException)
{
// In many cases the user will put in a plain old filename that cannot be found so assume that
// this is the problem rather than confusing the issue with a UriFormatException
throw new Exception(String.Format("Cannot find file {0}", path));
}
}
}
public static Stream URIFetch(Uri uri)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
// request.Credentials = credentials;
request.ContentLength = 0;
request.KeepAlive = false;
WebResponse response = request.GetResponse();
Stream file = response.GetResponseStream();
// justincc: gonna ignore the content type for now and just try anything
//if (response.ContentType != "application/x-oar")
// throw new Exception(String.Format("{0} does not identify an OAR file", uri.ToString()));
if (response.ContentLength == 0)
throw new Exception(String.Format("{0} returned an empty file", uri.ToString()));
// return new BufferedStream(file, (int) response.ContentLength);
return new BufferedStream(file, 1000000);
}
} }
} }

View File

@ -78,7 +78,7 @@ namespace OpenSim.Region.CoreModules.World.Archiver
try try
{ {
m_loadStream = new GZipStream(GetStream(loadPath), CompressionMode.Decompress); m_loadStream = new GZipStream(ArchiveHelpers.GetStream(loadPath), CompressionMode.Decompress);
} }
catch (EntryPointNotFoundException e) catch (EntryPointNotFoundException e)
{ {
@ -470,68 +470,6 @@ namespace OpenSim.Region.CoreModules.World.Archiver
return true; return true;
} }
/// <summary>
/// Resolve path to a working FileStream
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private Stream GetStream(string path)
{
if (File.Exists(path))
{
return new FileStream(path, FileMode.Open, FileAccess.Read);
}
else
{
try
{
Uri uri = new Uri(path);
if (uri.Scheme == "file")
{
return new FileStream(uri.AbsolutePath, FileMode.Open, FileAccess.Read);
}
else
{
if (uri.Scheme != "http")
throw new Exception(String.Format("Unsupported URI scheme ({0})", path));
// OK, now we know we have an HTTP URI to work with
return URIFetch(uri);
}
}
catch (UriFormatException)
{
// In many cases the user will put in a plain old filename that cannot be found so assume that
// this is the problem rather than confusing the issue with a UriFormatException
throw new Exception(String.Format("Cannot find file {0}", path));
}
}
}
private static Stream URIFetch(Uri uri)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
// request.Credentials = credentials;
request.ContentLength = 0;
request.KeepAlive = false;
WebResponse response = request.GetResponse();
Stream file = response.GetResponseStream();
// justincc: gonna ignore the content type for now and just try anything
//if (response.ContentType != "application/x-oar")
// throw new Exception(String.Format("{0} does not identify an OAR file", uri.ToString()));
if (response.ContentLength == 0)
throw new Exception(String.Format("{0} returned an empty file", uri.ToString()));
// return new BufferedStream(file, (int) response.ContentLength);
return new BufferedStream(file, 1000000);
}
/// <summary> /// <summary>
/// Load oar control file /// Load oar control file
/// </summary> /// </summary>