public override UploadResult Upload(Stream stream, string fileName) { if (customUploader.RequestType != CustomUploaderRequestType.POST) { throw new Exception("'Request type' must be 'POST' when using custom file uploader."); } if (string.IsNullOrEmpty(customUploader.FileFormName)) { throw new Exception("'File form name' must be not empty when using custom file uploader."); } if (string.IsNullOrEmpty(customUploader.RequestURL)) { throw new Exception("'Request URL' must be not empty."); } UploadResult result = UploadData(stream, customUploader.RequestURL, fileName, customUploader.FileFormName, customUploader.ParseArguments()); if (result.IsSuccess) { customUploader.Parse(result.Response); result.URL = customUploader.ResultURL; result.ThumbnailURL = customUploader.ResultThumbnailURL; result.DeletionURL = customUploader.ResultDeletionURL; } return(result); }
public override UploadResult UploadText(string text, string fileName) { if (string.IsNullOrEmpty(customUploader.RequestURL)) { throw new Exception("'Request URL' must be not empty."); } if ((customUploader.RequestType == CustomUploaderRequestType.GET || string.IsNullOrEmpty(customUploader.FileFormName)) && (customUploader.Arguments == null || !customUploader.Arguments.Any(x => x.Value.Contains("%input") || x.Value.Contains("$input$")))) { throw new Exception("Atleast one '%input' or '$input$' required for argument value when using GET or non-file POST."); } UploadResult result = new UploadResult(); customUploader.Input = text; Dictionary <string, string> args = customUploader.ParseArguments(); if (customUploader.RequestType == CustomUploaderRequestType.POST) { if (string.IsNullOrEmpty(customUploader.FileFormName)) { result.Response = SendPostRequest(customUploader.RequestURL, args, customUploader.ResponseType); } else { byte[] byteArray = Encoding.UTF8.GetBytes(text); using (MemoryStream stream = new MemoryStream(byteArray)) { result = UploadData(stream, customUploader.RequestURL, fileName, customUploader.FileFormName, args); } } } else if (customUploader.RequestType == CustomUploaderRequestType.GET) { result.Response = SendGetRequest(customUploader.RequestURL, args, customUploader.ResponseType); } if (!string.IsNullOrEmpty(result.Response)) { customUploader.Parse(result.Response); result.URL = customUploader.ResultURL; result.ThumbnailURL = customUploader.ResultThumbnailURL; result.DeletionURL = customUploader.ResultDeletionURL; } return(result); }
public override UploadResult ShortenURL(string url) { if (customUploader.RequestType == CustomUploaderRequestType.POST && !string.IsNullOrEmpty(customUploader.FileFormName)) { throw new Exception("'File form name' cannot be used with custom URL shortener."); } if (string.IsNullOrEmpty(customUploader.RequestURL)) { throw new Exception("'Request URL' must be not empty."); } if (customUploader.Arguments == null || !customUploader.Arguments.Any(x => x.Value.Contains("%input") || x.Value.Contains("$input$"))) { throw new Exception("Atleast one '%input' or '$input$' required for argument value when using custom URL shortener."); } UploadResult result = new UploadResult { URL = url }; customUploader.Input = url; Dictionary <string, string> args = customUploader.ParseArguments(); if (customUploader.RequestType == CustomUploaderRequestType.POST) { result.Response = SendPostRequest(customUploader.RequestURL, args, customUploader.ResponseType); } else if (customUploader.RequestType == CustomUploaderRequestType.GET) { result.Response = SendGetRequest(customUploader.RequestURL, args, customUploader.ResponseType); } if (!string.IsNullOrEmpty(result.Response)) { customUploader.Parse(result.Response); result.ShortenedURL = customUploader.ResultURL; result.ThumbnailURL = customUploader.ResultThumbnailURL; result.DeletionURL = customUploader.ResultDeletionURL; } return(result); }