using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
namespace BlackBlade.ListActions.DownloadAsZip
{
public class DownloadZipFileStatus : System.Web.UI.Page
{
protected Literal ltrClientRedirect;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new EventHandler(DownloadZipFileStatus_Load);
}
void DownloadZipFileStatus_Load(object sender, EventArgs e)
{
//this page expects the following HTTP request parameters
// SID: Guid of the SharePoint Web Front End server where the zip file is located
// ZipPath: Path on the SID where the zip file is located. This path
// should be relative to the "\\SID\admin$\temp\zip\" folder.
// LastSize: The previous size of the zip file in bytes. If the current size
// is the same as the last size, then the file zip process
// is complete
//get the parameters from the request
Guid serverID;
try
{
serverID = new Guid(this.Request["SID"]);
}
catch (Exception ex)
{
throw new ArgumentNullException("SID", ex);
}
string zipFilePath = this.Request["ZipPath"];
if (string.IsNullOrEmpty(zipFilePath))
throw new ArgumentNullException("ZipPath");
//get the server name from the sharepoint farm
SPFarm farm = SPFarm.Local;
string serverName = null;
try
{
serverName = farm.Servers[serverID].Name;
}
catch (Exception ex)
{
throw new ArgumentException("The " + serverID.ToString() + " is not a valid server id in this farm.", "SID", ex);
}
long lngLastSize = 0;
try
{
lngLastSize = long.Parse(this.Request["LastSize"]);
}
catch (Exception)
{
}
//generate the complete path for the zip file
string fullZipFilePath = @"\\" + serverName + @"\admin$\temp\zip\" + zipFilePath;
//ensure that the temporary directory exists
System.IO.FileInfo fi = new System.IO.FileInfo(fullZipFilePath);
if (!fi.Directory.Exists)
throw new System.IO.FileNotFoundException("Could not access the temporary location of the compressed file.", fullZipFilePath);
//check to see if the zip file size has changed since last time
long lngSize = fi.Length;
if (lngLastSize == lngSize)
//the processing is finished. redirect to the results page
this.Response.Redirect(string.Format("DownloadZipFileResult.aspx?SID={0}&ZipPath={1}",
SPHttpUtility.UrlKeyValueEncode(serverID),
SPHttpUtility.UrlKeyValueEncode(zipFilePath)));
else
{
//the processing is still going. check back in 3 seconds
string strClientRedirect =
"<script type=\"text/javascript\"> \r\n" +
" window.setTimeout(\"window.location = '{0}'\", 3000); \r\n" +
"</script>";
string strRedirectUrl = string.Format("DownloadZipFileStatus.aspx?SID={0}&ZipPath={1}&LastSize={2}",
SPHttpUtility.UrlKeyValueEncode(serverID.ToString()),
SPHttpUtility.UrlKeyValueEncode(zipFilePath),
SPHttpUtility.UrlKeyValueEncode(lngSize.ToString()));
this.ltrClientRedirect.Text = string.Format(strClientRedirect, strRedirectUrl);
}
}
}
}