public WriteResult Write(WeedFSFile file, Location location, byte[] dataToUpload, string fileName) { if (dataToUpload.Length == 0) { throw new WeedFSException("Cannot write a 0-length data"); } return(Write(file, location, fileName, null, dataToUpload)); }
public WriteResult Write(WeedFSFile file, Location location, FileInfo fileToUpload) { if (fileToUpload.Length == 0) { throw new WeedFSException("Cannot write a 0-length file"); } return(Write(file, location, fileToUpload.Name, fileToUpload)); }
public Stream Read(WeedFSFile file, Location location) { var url = new StringBuilder(); if (!location.publicUrl.Contains("http")) { url.Append("http://"); } url.AppendFormat("{0}/{1}", location.publicUrl, file.Fid); if (file.Version > 0) { url.Append('_').Append(file.Version); } var cts = new CancellationTokenSource(); using (var response = _httpClient.GetAsync(url.ToString(), cts.Token)) { var result = response.Result; var statusCode = result.StatusCode; if (statusCode == HttpStatusCode.NotFound) { cts.Cancel(); throw new WeedFSFileNotFoundException(file, location); } if (statusCode != HttpStatusCode.OK) { if (!response.IsCanceled) { cts.Cancel(); } throw new WeedFSException($"Error reading file {file.Fid} on {location.publicUrl}: {statusCode} {result.ReasonPhrase}"); } return(response.Result.Content.ReadAsStreamAsync().Result); } }
public WriteResult Write(WeedFSFile file, Location location, Stream inputToUpload, string fileName) { return(Write(file, location, fileName, null, null, inputToUpload)); }
private WriteResult Write(WeedFSFile file, Location location, string fileName = null, FileInfo fileToUpload = null, byte[] dataToUpload = null, Stream inputToUpload = null) { var url = new StringBuilder(); if (!location.publicUrl.Contains("http")) { url.Append("http://"); } url.AppendFormat("{0}/{1}", location.publicUrl, file.Fid); if (file.Version > 0) { url.Append('_').Append(file.Version); } byte[] buffer; if (fileToUpload != null) { if (string.IsNullOrWhiteSpace(fileName)) { fileName = fileToUpload.Name; } var stream = fileToUpload.OpenRead(); buffer = StreamHelper.StreamToBytes(stream); } else if (dataToUpload != null) { buffer = dataToUpload; } else { buffer = StreamHelper.StreamToBytes(inputToUpload); } var multipart = new MultipartFormDataContent(); multipart.Add(new ByteArrayContent(buffer) { Headers = { ContentType = new MediaTypeHeaderValue("application/octet-stream"), ContentDisposition = new ContentDispositionHeaderValue("form-data") } }, "file", SanitizeFileName(fileName)); var fileUrl = url.ToString(); using (var response = _httpClient.PostAsync(fileUrl, multipart)) { var content = response.Result.Content.ReadAsStringAsync().Result; var result = JsonSerializer.DeserializeFromString <WriteResult>(content); result.url = fileUrl; if (!string.IsNullOrWhiteSpace(result.error)) { throw new WeedFSException(result.error); } return(result); } }