/// <summary> /// /// </summary> /// <param name="uri"></param> /// <param name="method"></param> /// <returns></returns> protected virtual WebRequest CreateRequest(Uri uri, WebRequestMethod requestMethod) { var _request = FtpWebRequest.Create(uri) as FtpWebRequest; _request.KeepAlive = true; _request.UsePassive = false; _request.Method = WebRequestHelpers.WebRequestMethodToFtpMethodString(requestMethod); _request.Credentials = FileService.Credential; return(_request); }
/// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="destination"></param> public void DownloadFileAsync(string source, FileStream destination) { var _requestMethod = WebRequestMethod.DownloadFile; var _resourceName = WebRequestHelpers.ReplacePathDelimiterToUriDelimiter(source); var _uri = CreateUniformResourceIdentifier(_resourceName); var _request = CreateRequest(_uri, _requestMethod); var _length = GetContentLength(_uri); var _state = new FileDownloadState(_request, BUFFER_SIZE, null, destination, _length); _request.BeginGetResponse(new AsyncCallback(_BeginGetResponseCallback), _state); }
/// <summary> /// /// </summary> /// <param name="directoryPath"></param> /// <returns></returns> public IExternalResult CreateDirectory(string directoryPath) { var _requestMethod = WebRequestMethod.MakeDirectory; var _directoryName = WebRequestHelpers.ReplacePathDelimiterToUriDelimiter(directoryPath); try { if (-1 == _directoryName.IndexOf("/")) { var _uri = CreateUniformResourceIdentifier(_directoryName); var _request = CreateRequest(_uri, _requestMethod); var _requestResult = GetRequestResult(_request.GetResponse(), _requestMethod); _request.Abort(); return(_requestResult); } else { var _split = _directoryName.Split('/'); var _current = (string)null; for (var i = 0; i < _split.Length; i++) { _current = (string.IsNullOrEmpty(_current)) ? _split[i] : string.Format("{0}/{1}", _current, _split[i]); if (!ExistDirectory(_current)) { var _uri = CreateUniformResourceIdentifier(_current); var _request = CreateRequest(_uri, _requestMethod); var _requestResult = GetRequestResult(_request.GetResponse(), _requestMethod); _request.Abort(); if (!_requestResult.ErrorCode.Equals(0)) { throw new InvalidOperationException(_requestResult.ErrorText); } } } return(new OperationResult()); } } catch (Exception ex) { return(new OperationResult(ex.Message, -1)); } }
/// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="destination"></param> public void UploadFileAsync(Stream source, string destination, object userToken = null) { var _requestMethod = WebRequestMethod.UploadFile; var _directoryName = Path.GetDirectoryName(destination); if (!string.IsNullOrEmpty(_directoryName)) { CreateDirectory(_directoryName); } var _uri = CreateUniformResourceIdentifier(WebRequestHelpers.ReplacePathDelimiterToUriDelimiter(destination)); var _request = CreateRequest(_uri, _requestMethod); var _state = new FileUploadAsyncState(_request, BUFFER_SIZE, source, null, source.Length, userToken); _request.BeginGetRequestStream(new AsyncCallback(_BeginGetRequestStream), _state); }
/// <summary> /// /// </summary> /// <param name="filePath"></param> /// <returns></returns> public bool ExistFile(string filePath) { var _requestMethod = WebRequestMethod.ExistDirectory; var _resourceName = WebRequestHelpers.ReplacePathDelimiterToUriDelimiter(filePath); var _uri = CreateUniformResourceIdentifier(_resourceName); var _request = CreateRequest(_uri, _requestMethod); try { return(GetRequestResult(_request.GetResponse(), _requestMethod).ErrorCode.Equals(0)); } catch (Exception) { return(false); } finally { _request.Abort(); } }
/// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="destination"></param> /// <returns></returns> public IExternalResult UploadFile(Stream source, string destination) { var _requestMethod = WebRequestMethod.UploadFile; var _directoryName = Path.GetDirectoryName(destination); if (!string.IsNullOrEmpty(_directoryName)) { CreateDirectory(_directoryName); } var _uri = CreateUniformResourceIdentifier(WebRequestHelpers.ReplacePathDelimiterToUriDelimiter(destination)); var _request = CreateRequest(_uri, _requestMethod); using (var _destination = _request.GetRequestStream()) { var _buffer = new byte[source.Length]; var _bytesToRead = (int)source.Length; var _bytesRead = 0; while (0 < _bytesToRead) { var _read = 0; if (0 < (_read = source.Read(_buffer, _bytesRead, _bytesToRead))) { _bytesRead += _read; _bytesToRead -= _read; continue; } break; } _destination.Write(_buffer, 0, _buffer.Length); } return(GetRequestResult(_request.GetResponse(), _requestMethod)); }
/// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="destination"></param> /// <returns></returns> public IExternalResult DownloadFile(string source, FileStream destination) { var _requestMethod = WebRequestMethod.DownloadFile; var _resourceName = WebRequestHelpers.ReplacePathDelimiterToUriDelimiter(source); var _uri = CreateUniformResourceIdentifier(_resourceName); var _request = CreateRequest(_uri, _requestMethod); var _length = GetContentLength(_uri); var _response = _request.GetResponse(); var _source = _response.GetResponseStream(); using (var _state = new FileDownloadState(_request, BUFFER_SIZE, _source, destination, _length)) { while (0 < (_state.BytesDownloaded = _state.Source.Read(_state.ByteBuffer, 0, _state.ByteBuffer.Length))) { _state.Destination.Write(_state.ByteBuffer, 0, _state.BytesDownloaded); } return(GetRequestResult(_response, _requestMethod)); } }