public override async Task UploadFileAsync(string path, byte[] content, CancellationToken ctk = default) { await _semaphore.WaitAsync(ctk); try { using (var ostrm = _client.Stor(path, FtpTransferMode.Binary)) { await ostrm.WriteAsync(content, 0, content.Length, ctk); await ostrm.FlushAsync(ctk); } } finally { _semaphore.Release(); } }
public bool Upload(string rootRemoteDirectory = null) { if (ftp == null) { return(false); } foreach (var dir in this.directorySet) { string uri = rootRemoteDirectory + "/" + dir.Substring(2); #if VERY_DEBUG Debug.WriteLine("MKD: " + uri); #endif try { ftp.Mkd(uri); } catch (ArxOne.Ftp.Exceptions.FtpFileException ex) { if (ex.Code != 550) { Trace.WriteLine(ex.Message); } } } using (MemoryStream commandBuilder = new MemoryStream()) { string command = $"#!/bin/sh\ncd \"{rootRemoteDirectory}\"\n"; commandBuilder.Write(Encoding.UTF8.GetBytes(command), 0, command.Length); long currentPosition = 0; foreach (var afi in localSet) { if (skipFiles != null && Array.IndexOf(skipFiles, Path.GetFileName(afi.FilePath)) != -1) { continue; } Stream inStream = null; bool owned = true; if (!string.IsNullOrEmpty(afi.LocalFilePath)) { inStream = new FileStream(afi.LocalFilePath, FileMode.Open); } else if (afi.FileStream != null) { inStream = afi.FileStream; inStream.Position = 0; owned = false; } else if (this.rootDirectory != null) { string inPath = new Uri(this.rootDirectory + "/" + afi.FilePath).LocalPath; inStream = new FileStream(inPath, FileMode.Open); } if (inStream != null) { string uri = rootRemoteDirectory + "/" + afi.FilePath.Substring(2); using (Stream outStream = ftp.Stor(uri)) { #if VERY_DEBUG Debug.WriteLine("STOR: " + uri); #endif byte[] buf = new byte[transferChunkSize]; int read, totalRead = 0; while ((read = inStream.Read(buf, 0, buf.Length)) > 0) { outStream.Write(buf, 0, read); totalRead += read; OnReadProgress(currentPosition + totalRead, totalSize, afi.FilePath); } } if (owned) { inStream.Dispose(); } command = string.Format("touch -m -t {1} '{0}'\n", afi.FilePath.Replace("'", @"'\''"), afi.ModifiedTime.ToString("yyyyMMddHHmm.ss")); commandBuilder.Write(Encoding.UTF8.GetBytes(command), 0, command.Length); } else { Trace.WriteLine($"Error with file \"{afi.FilePath}\", no input data."); OnFileInputError(afi.FilePath); } currentPosition += afi.FileSize; OnReadProgress(currentPosition, totalSize, afi.FilePath); } // adjust time pointers! hakchi.RunTemporaryScript(commandBuilder, "modtime.sh"); } return(true); }