public static int Upload(string path, bool overwrite, long port = 0) { // if the path to receive the file doesn't exist, try to create it. if (!Directory.Exists(path)) { try { Directory.CreateDirectory(path); } catch (Exception) { throw new IOException($"Can't create receive directory {path}. " + "There was either a permission problem, or the path is invalid." ); } } if (!ReadWriteChecks.CheckWriteToDir(path)) { throw new IOException($"Can't write to {path}, there was either a permission problem or the path doesn't exist."); } TcpListener server = new TcpListener(IPAddress.Any, (int)port); // if a port is specified, use SO_REUSEADDR on the socket if (port != 0) { server.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); } server.Start(1); Task.Run(() => HelperFunctions.LargeFileTransfers.Upload(path, server, overwrite)); return(((IPEndPoint)server.LocalEndpoint).Port); }
public static long[] Download(string path, long port = 0) { if (!ReadWriteChecks.CheckReadFromFileOrDir(path)) { throw new IOException($"Can't read from {path}, there was either a permission problem, or the path doesn't exist."); } TcpListener server = new TcpListener(IPAddress.Any, (int)port); // if a port is specified, use SO_REUSEADDR on the socket if (port != 0) { server.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); } server.Start(1); // get length of all files in the directory we're about to download // (total directory size) long size = 0; if (path.Contains("*") || path.Contains("?")) { // we're handling a glob expression size = SimpleDUTCommonLibrary.GlobFunctions.Glob(path) .Where(x => !File.GetAttributes(x).HasFlag(FileAttributes.Directory)) .Select(x => (new FileInfo(x)).Length) .Sum(); } else if (File.GetAttributes(path).HasFlag(FileAttributes.Directory)) { size = Directory.GetFiles(path, "*", SearchOption.AllDirectories) .Aggregate <string, long>(0, (acc, current) => acc + (new FileInfo(current)).Length); } else { size = (new FileInfo(path)).Length; } Task.Run(() => HelperFunctions.LargeFileTransfers.Download(path, server)); return(new long[] { ((IPEndPoint)server.LocalEndpoint).Port, size }); }