You are on page 1of 7

using System;

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Scribd.Net.Properties;
using System.IO;
using System.Net;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Xml;
namespace Scribd.Net
{
public sealed class Service
{
[CompilerGenerated()]
private static string m_APIKey;
[CompilerGenerated()]
private static bool m_EnforceSigning;
[CompilerGenerated()]
private Scribd.Net.User m_InternalUser;
[CompilerGenerated()]
private static string m_PublisherID;
[CompilerGenerated()]
private static string m_SecretKey;
[CompilerGenerated()]
private static string m_TemporaryDirectoryPath;
[CompilerGenerated()]
private static IWebProxy m_WebProxy;
public static event EventHandler<ServicePostEventArgs> AfterPost;
public static event EventHandler<ServicePostEventArgs> BeforePost;
public static event EventHandler<ScribdEventArgs> Error;
private Service()
{
}
internal void _client_UploadFileCompleted(object sender, UploadFileCompl
etedEventArgs e)
{
try
{
Document document__1 = new Document();
Response response = new Response(Encoding.Default.GetString(e.Re
sult));
if (response.Status != "ok")
{
foreach (int num in response.ErrorList.Keys)
{
OnErrorOccurred(num, response.ErrorList[num]);
}
}
else if (((response != null) && response.HasChildNodes) && (resp
onse.ErrorList.Count < 1))
{
XmlNode node = response.SelectSingleNode("rsp");
document__1.DocumentId = int.Parse(node.SelectSingleNode("do
c_id").InnerText);
document__1.AccessKey = node.SelectSingleNode("access_key").
InnerText;
if (node.SelectSingleNode("secret_password") != null)
{
document__1.AccessType = AccessTypes.Private;
document__1.SecretPassword = node.SelectSingleNode("secr
et_password").InnerText;
}
}
Document.OnUploaded(document__1);
}
catch (Exception exception)
{
OnErrorOccurred(0x29a, exception.ToString());
}
}
internal void _client_UploadProgressChanged(object sender, UploadProgres
sChangedEventArgs e)
{
Document.OnUploadProgressChanged(sender, e);
}
static internal byte[] FileUploader(string fileName, string url, string
contentType)
{
return FileUploader(fileName, url, contentType, new CookieContainer(
));
}
internal static byte[] FileUploader(string fileName, string url, string
contentType, CookieContainer cookies)
{
if ((contentType == null) || (contentType.Length == 0))
{
contentType = "application/octet-stream";
}
Uri requestUri = new Uri(url);
string str = "----------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUr
i);
request.CookieContainer = cookies;
request.ContentType = "multipart/form-data; boundary=" + str;
request.Method = "POST";
request.KeepAlive = false;
request.AllowWriteStreamBuffering = true;
request.Timeout = -1;
request.Accept = "*/*";
request.AllowAutoRedirect = false;
request.UserAgent = UserAgent;
request.ProtocolVersion = HttpVersion.Version10;
StringBuilder builder = new StringBuilder();
builder.Append("--");
builder.Append(str);
builder.Append("\r\n");
builder.Append("Content-Disposition: form-data; name=\"file\"");
builder.Append("\"; filename=\"");
builder.Append(Path.GetFileName(fileName));
builder.Append("\"\r\n");
builder.Append("Content-Type: ");
builder.Append(contentType);
builder.Append("\r\n\r\n");
string s = builder.ToString();
byte[] bytes = Encoding.UTF8.GetBytes(s);
byte[] buffer = Encoding.ASCII.GetBytes("\r\n--" + str + "\r\n");
FileStream stream = new FileStream(fileName, FileMode.Open, FileAcce
ss.Read);
long num = (bytes.Length + stream.Length) + buffer.Length;
request.ContentLength = num;
Stream requestStream = request.GetRequestStream();
for (int i = 0; i < bytes.Length; i++)
{
requestStream.WriteByte(bytes[i]);
Document.OnUploadProgressChanged(null, null);
}
byte[] buffer3 = new byte[Math.Min(0x1000, (int)stream.Length)];
int count = 0;
while ((count = stream.Read(buffer3, 0, buffer3.Length)) != 0)
{
requestStream.Write(buffer3, 0, count);
}
requestStream.Write(buffer, 0, buffer.Length);
string str3 = new StreamReader(request.GetResponse().GetResponseStre
am()).ReadToEnd();
ASCIIEncoding encoding = new ASCIIEncoding();
return encoding.GetBytes(str3);
}
static internal void OnAfterPost(ServicePostEventArgs arguments)
{
if (AfterPost != null)
{
AfterPost(Instance, arguments);
}
}
static internal void OnBeforePost(ServicePostEventArgs arguments)
{
if (BeforePost != null)
{
BeforePost(Instance, arguments);
}
}
static internal void OnErrorOccurred(int errorCode, string message)
{
if (Error != null)
{
Error(Instance, new ScribdEventArgs(errorCode, message));
}
}
internal void PostFileUploadRequest(Request request)
{
ServicePostEventArgs arguments = new ServicePostEventArgs(request.RE
STCall, request.MethodName);
OnBeforePost(arguments);
if (!arguments.Cancel)
{
if (string.IsNullOrEmpty(m_APIKey))
{
OnErrorOccurred(0x2710,Resources.ERR_NO_APIKEY);
}
else if (string.IsNullOrEmpty(m_SecretKey))
{
OnErrorOccurred(0x2711, Resources.ERR_NO_SECRETKEY);
}
else
{
request.User = this.m_InternalUser;
try
{
using (WebClient client = new WebClient())
{
if (m_WebProxy != null)
{
client.Proxy = m_WebProxy;
}
if (request.MethodName == "docs.upload")
{
string fileName = request.Parameters["file"];
request.Parameters.Remove("file");
client.UploadProgressChanged += new UploadProgre
ssChangedEventHandler(this._client_UploadProgressChanged);
client.UploadFileCompleted += new UploadFileComp
letedEventHandler(this._client_UploadFileCompleted);
client.UploadFileAsync(new Uri(request.RESTCall)
, "POST", fileName);
}
}
}
catch (Exception exception)
{
OnErrorOccurred(0x29a, exception.ToString());
}
finally
{
OnAfterPost(arguments);
}
}
}
}
internal Response PostRequest(Request request)
{
Response response = null;
ServicePostEventArgs arguments = new ServicePostEventArgs(request.RE
STCall, request.MethodName);
OnBeforePost(arguments);
if (!arguments.Cancel)
{
if (string.IsNullOrEmpty(m_APIKey))
{
OnErrorOccurred(0x2710, Resources.ERR_NO_APIKEY);
return response;
}
if (string.IsNullOrEmpty(m_SecretKey))
{
OnErrorOccurred(0x2711, Resources.ERR_NO_SECRETKEY);
return response;
}
request.User = this.m_InternalUser;
try
{
using (WebClient client = new WebClient())
{
client.Headers.Add("user-agent", UserAgent);
if (m_WebProxy != null)
{
client.Proxy = m_WebProxy;
}
string xml = string.Empty;
if (request.MethodName == "docs.upload")
{
string fileName = request.Parameters["file"];
request.Parameters.Remove("file");
byte[] bytes = FileUploader(fileName, request.RESTCa
ll, string.Empty);
xml = Encoding.Default.GetString(bytes);
}
else
{
xml = client.DownloadString(request.ToString());
}
response = new Response(xml);
if (response.Status != "ok")
{
foreach (int num in response.ErrorList.Keys)
{
OnErrorOccurred(num, response.ErrorList[num]);
}
}
arguments.ResponseXML = response.InnerXml;
}
}
catch (Exception exception)
{
OnErrorOccurred(0x29a, exception.ToString());
}
finally
{
OnAfterPost(arguments);
}
}
return response;
}
public static string Slurpify(string url)
{
return Slurpify(url, DisplayMode.Scribd, false);
}
public static string Slurpify(string url, DisplayMode displayMode, bool
isPrivate)
{
if (!string.IsNullOrEmpty(m_PublisherID))
{
string format = "http://www.scribd.com/slurp?url={0}&display_mod
e={1}&privacy={2}&publisher_id={3}";
return string.Format(format, new object[] {
url,
displayMode.ToString().ToLower(),
isPrivate ? "private" : "public",
m_PublisherID
});
}
OnErrorOccurred(0x2714, Resources.ERR_NO_PUBLISHERID);
return string.Empty;
}
public static string APIKey
{
[CompilerGenerated()]
get { return m_APIKey; }
[CompilerGenerated()]
set { m_APIKey = value; }
}
public static string APIUrl
{
get { return Resources.API_URL; }
}
public static bool EnforceSigning
{
[CompilerGenerated()]
get { return m_EnforceSigning; }
[CompilerGenerated()]
set { m_EnforceSigning = value; }
}
static internal Service Instance
{
get { return InternalService.m_instance; }
}
internal Scribd.Net.User InternalUser
{
[CompilerGenerated()]
get { return this.m_InternalUser; }
[CompilerGenerated()]
set { this.m_InternalUser = value; }
}
public bool IsUserLoggedIn
{
get { return ((this.m_InternalUser != null) && !string.IsNullOrEmpty
(this.m_InternalUser.Name)); }
}
public static string PublisherID
{
[CompilerGenerated()]
get { return m_PublisherID; }
[CompilerGenerated()]
set { m_PublisherID = value; }
}
public static string SecretKey
{
[CompilerGenerated()]
get { return m_SecretKey; }
[CompilerGenerated()]
set { m_SecretKey = value; }
}
public static string TemporaryDirectoryPath
{
[CompilerGenerated()]
get { return m_TemporaryDirectoryPath; }
[CompilerGenerated()]
set { m_TemporaryDirectoryPath = value; }
}
public static Scribd.Net.User User
{
get { return Instance.InternalUser; }
}
public static string UserAgent
{
get { return string.Format("Scribd.Net Library {0}", Version.ToStrin
g()); }
}
public static System.Version Version
{
get { return Assembly.GetExecutingAssembly().GetName().Version; }
}
public static IWebProxy WebProxy
{
[CompilerGenerated()]
get { return m_WebProxy; }
[CompilerGenerated()]
set { m_WebProxy = value; }
}
private class InternalService
{
static internal Service m_instance = new Service();
static InternalService()
{
//m_instance.InternalUser = new User();
Service.InternalService.m_instance = new Service();
Service.InternalService.m_instance.InternalUser = new User();
}
}
}
private static T InlineAssignHelper<T>(ref T target, T value)
{
target = value;
return value;
}
}
}