public override UploadResult Upload(Stream stream, string fileName) { if (string.IsNullOrEmpty(APIURL)) { throw new Exception("Seafile API URL is empty."); } if (string.IsNullOrEmpty(AuthToken)) { throw new Exception("Seafile Authentication Token is empty."); } if (string.IsNullOrEmpty(Path)) { Path = "/"; } else { char pathLast = Path[Path.Length - 1]; if (pathLast != '/') { Path += "/"; } } string url = URLHelpers.FixPrefix(APIURL); url = URLHelpers.CombineURL(APIURL, "repos/" + RepoID + "/upload-link/?format=json"); NameValueCollection headers = new NameValueCollection(); headers.Add("Authorization", "Token " + AuthToken); SSLBypassHelper sslBypassHelper = null; try { if (IgnoreInvalidCert) { sslBypassHelper = new SSLBypassHelper(); } string response = SendRequest(HttpMethod.GET, url, null, headers); string responseURL = response.Trim('"'); Dictionary<string, string> args = new Dictionary<string, string>(); args.Add("filename", fileName); args.Add("parent_dir", Path); UploadResult result = UploadData(stream, responseURL, fileName, "file", args, headers); if (!IsError) { if (CreateShareableURL && !IsLibraryEncrypted) { AllowReportProgress = false; result.URL = ShareFile(Path + fileName, result.Response.Trim('"')); } else { result.IsURLExpected = false; } } return result; } finally { if (sslBypassHelper != null) { sslBypassHelper.Dispose(); } } }
public bool ValidatePath(string path) { string url = URLHelpers.FixPrefix(APIURL); url = URLHelpers.CombineURL(APIURL, "repos/" + RepoID + "/dir/?p=" + path + "&format=json"); NameValueCollection headers = new NameValueCollection(); headers.Add("Authorization", "Token " + AuthToken); SSLBypassHelper sslBypassHelper = null; try { if (IgnoreInvalidCert) { sslBypassHelper = new SSLBypassHelper(); } string response = SendRequest(HttpMethod.GET, url, null, headers); if (!string.IsNullOrEmpty(response)) { return true; } return false; } finally { if (sslBypassHelper != null) { sslBypassHelper.Dispose(); } } }
public string GetOrMakeDefaultLibrary(string authtoken = null) { string url = URLHelpers.FixPrefix(APIURL); url = URLHelpers.CombineURL(APIURL, "default-repo/?format=json"); NameValueCollection headers = new NameValueCollection(); headers.Add("Authorization", "Token " + (authtoken == null ? AuthToken : authtoken)); SSLBypassHelper sslBypassHelper = null; try { if (IgnoreInvalidCert) { sslBypassHelper = new SSLBypassHelper(); } string response = SendRequest(HttpMethod.GET, url, null, headers); if (!string.IsNullOrEmpty(response)) { SeafileDefaultLibraryObj JsonResponse = JsonConvert.DeserializeObject<SeafileDefaultLibraryObj>(response); return JsonResponse.repo_id; } return null; } finally { if (sslBypassHelper != null) { sslBypassHelper.Dispose(); } } }
public string ShareFile(string path, string id = null) { string url = URLHelpers.FixPrefix(APIURL); url = URLHelpers.CombineURL(APIURL, "repos/" + RepoID + "/file/shared-link/"); Dictionary<string, string> args = new Dictionary<string, string>(); if (!String.IsNullOrEmpty(SharePassword)) args.Add("password", SharePassword); args.Add("p", path); args.Add("format", "json"); args.Add("expire", ShareDaysToExpire.ToString()); NameValueCollection headers = new NameValueCollection(); headers.Add("Authorization", "Token " + AuthToken); SSLBypassHelper sslBypassHelper = null; try { if (IgnoreInvalidCert) { sslBypassHelper = new SSLBypassHelper(); } //had to do this to get the ContentLength header to use for the PUT request string boundary = new string('-', 20) + DateTime.Now.Ticks.ToString("x"); byte[] POSTDATA; using (MemoryStream stream = new MemoryStream()) { byte[] bytes; if (args != null) { foreach (KeyValuePair<string, string> content in args) { if (!string.IsNullOrEmpty(content.Key) && !string.IsNullOrEmpty(content.Value)) { string format = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", boundary, content.Key, content.Value); bytes = Encoding.UTF8.GetBytes(format); stream.Write(bytes, 0, bytes.Length); } } bytes = Encoding.UTF8.GetBytes(string.Format("--{0}--\r\n", boundary)); stream.Write(bytes, 0, bytes.Length); } POSTDATA = stream.ToArray(); } MemoryStream dataStream = new MemoryStream(); dataStream.Write(POSTDATA, 0, POSTDATA.Length); HttpWebResponse response = null; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.ContentLength = POSTDATA.Length; request.Accept = "application/json"; request.Method = "PUT"; request.Headers.Add(headers); request.UserAgent = "ShareX"; string contentType = "multipart/form-data"; request.AllowWriteStreamBuffering = HelpersOptions.CurrentProxy.IsValidProxy(); request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); request.ContentLength = dataStream.Length; if (!string.IsNullOrEmpty(boundary)) contentType += "; boundary=" + boundary; request.ContentType = contentType; request.CookieContainer = new CookieContainer(); request.KeepAlive = true; request.Pipelined = false; request.ProtocolVersion = HttpVersion.Version11; request.Proxy = HelpersOptions.CurrentProxy.GetWebProxy(); request.Timeout = -1; using (Stream requestStream = request.GetRequestStream()) { if (!TransferData(dataStream, requestStream)) { } } response = (HttpWebResponse)request.GetResponse(); string Location = response.Headers["Location"]; response.Close(); dataStream.Close(); if (!string.IsNullOrEmpty(Location)) { return Location; } return null; } finally { if (sslBypassHelper != null) { sslBypassHelper.Dispose(); } } }
public SeafileCheckAccInfoResponse GetAccountInfo() { string url = URLHelpers.FixPrefix(APIURL); url = URLHelpers.CombineURL(APIURL, "account/info/?format=json"); NameValueCollection headers = new NameValueCollection(); headers.Add("Authorization", "Token " + AuthToken); SSLBypassHelper sslBypassHelper = null; try { if (IgnoreInvalidCert) { sslBypassHelper = new SSLBypassHelper(); } string response = SendRequest(HttpMethod.GET, url, null, headers); if (!string.IsNullOrEmpty(response)) { SeafileCheckAccInfoResponse AccInfoResponse = JsonConvert.DeserializeObject<SeafileCheckAccInfoResponse>(response); return AccInfoResponse; } return null; } finally { if (sslBypassHelper != null) { sslBypassHelper.Dispose(); } } }
public List<SeafileLibraryObj> GetLibraries() { string url = URLHelpers.FixPrefix(APIURL); url = URLHelpers.CombineURL(APIURL, "repos/?format=json"); NameValueCollection headers = new NameValueCollection(); headers.Add("Authorization", "Token " + AuthToken); SSLBypassHelper sslBypassHelper = null; try { if (IgnoreInvalidCert) { sslBypassHelper = new SSLBypassHelper(); } string response = SendRequest(HttpMethod.GET, url, null, headers); if (!string.IsNullOrEmpty(response)) { List<SeafileLibraryObj> JsonResponse = JsonConvert.DeserializeObject<List<SeafileLibraryObj>>(response); return JsonResponse; } return null; } finally { if (sslBypassHelper != null) { sslBypassHelper.Dispose(); } } }
public bool DecryptLibrary(string libraryPassword) { string url = URLHelpers.FixPrefix(APIURL); url = URLHelpers.CombineURL(APIURL, "repos/" + RepoID + "/?format=json"); NameValueCollection headers = new NameValueCollection(); headers.Add("Authorization", "Token " + AuthToken); Dictionary<string, string> args = new Dictionary<string, string>(); args.Add("password", libraryPassword); SSLBypassHelper sslBypassHelper = null; try { if (IgnoreInvalidCert) { sslBypassHelper = new SSLBypassHelper(); } string response = SendRequest(HttpMethod.POST, url, args, headers); if (!string.IsNullOrEmpty(response)) { if (response == "\"success\"") { return true; } else { return false; } } return false; } finally { if (sslBypassHelper != null) { sslBypassHelper.Dispose(); } } }
public bool CheckAuthToken() { string url = URLHelpers.FixPrefix(APIURL); url = URLHelpers.CombineURL(APIURL, "auth/ping/?format=json"); NameValueCollection headers = new NameValueCollection(); headers.Add("Authorization", "Token " + AuthToken); SSLBypassHelper sslBypassHelper = null; try { if (IgnoreInvalidCert) { sslBypassHelper = new SSLBypassHelper(); } string response = SendRequest(HttpMethod.GET, url, null, headers); if (!string.IsNullOrEmpty(response)) { if (response == "\"pong\"") { return true; } } return false; } finally { if (sslBypassHelper != null) { sslBypassHelper.Dispose(); } } }
public bool CheckAPIURL() { string url = URLHelpers.FixPrefix(APIURL); url = URLHelpers.CombineURL(APIURL, "ping/?format=json"); SSLBypassHelper sslBypassHelper = null; try { if (IgnoreInvalidCert) { sslBypassHelper = new SSLBypassHelper(); } string response = SendRequest(HttpMethod.GET, url); if (!string.IsNullOrEmpty(response)) { if (response == "\"pong\"") { return true; } } return false; } finally { if (sslBypassHelper != null) { sslBypassHelper.Dispose(); } } }
public override UploadResult Upload(Stream stream, string fileName) { if (string.IsNullOrEmpty(Host)) { throw new Exception("ownCloud Host is empty."); } if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)) { throw new Exception("ownCloud Username or Password is empty."); } if (string.IsNullOrEmpty(Path)) { Path = "/"; } string path = URLHelpers.CombineURL(Path, fileName); string url = URLHelpers.CombineURL(Host, "remote.php/webdav", path); url = URLHelpers.FixPrefix(url); NameValueCollection headers = CreateAuthenticationHeader(Username, Password); SSLBypassHelper sslBypassHelper = null; try { if (IgnoreInvalidCert) { sslBypassHelper = new SSLBypassHelper(); } string response = SendRequestStream(url, stream, Helpers.GetMimeType(fileName), headers, method: HttpMethod.PUT); UploadResult result = new UploadResult(response); if (!IsError) { if (CreateShare) { AllowReportProgress = false; result.URL = ShareFile(path); } else { result.IsURLExpected = false; } } return result; } finally { if (sslBypassHelper != null) { sslBypassHelper.Dispose(); } } }
public string ShareFile(string path) { string url = URLHelpers.FixPrefix(APIURL); url = URLHelpers.CombineURL(APIURL, "repos", RepoID, "file/shared-link/"); Dictionary<string, string> args = new Dictionary<string, string>(); args.Add("p", path); args.Add("share_type", "download"); if (!string.IsNullOrEmpty(SharePassword)) args.Add("password", SharePassword); if (ShareDaysToExpire > 0) args.Add("expire", ShareDaysToExpire.ToString()); NameValueCollection headers = new NameValueCollection(); headers.Add("Authorization", "Token " + AuthToken); SSLBypassHelper sslBypassHelper = null; try { if (IgnoreInvalidCert) { sslBypassHelper = new SSLBypassHelper(); } return SendRequestURLEncoded(url, args, headers, method: HttpMethod.PUT, responseType: ResponseType.LocationHeader); } finally { if (sslBypassHelper != null) { sslBypassHelper.Dispose(); } } }