public void GetFileStream(NamedObjectStream stream, string uncPath, string contentFilename, long?startRange = null, long?endRange = null) { object fileHandle = new object(); FileStatus status; SMBLibrary.FileInformation fileInfo = null; var pathToFile = $"{uncPath}\\{contentFilename}"; _status = _smb2FileStore.CreateFile(out fileHandle, out status, pathToFile, AccessMask.GENERIC_READ, 0, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_NON_DIRECTORY_FILE, null); if (_status == NTStatus.STATUS_SUCCESS) { bool read = true; byte[] data = null; int start = (startRange != null && startRange.ToInt(0) >= 0) ? startRange.ToInt(0) : 0; int chunkSize = 65536; _smb2FileStore.GetFileInformation(out fileInfo, fileHandle, FileInformationClass.FileStandardInformation); //populate the stream metadata stream.Name = contentFilename; //is this a ranged call? if (startRange != null && endRange != null) { chunkSize = ((endRange - startRange) < chunkSize) ? (int)(endRange - startRange + 1) : chunkSize; int remainingBytesToRead = endRange.ToInt(0) - startRange.ToInt(0) + 1; while (read) { _smb2FileStore.ReadFile(out var chunk, fileHandle, start, chunkSize); stream.Write(chunk); start += chunkSize; remainingBytesToRead -= chunkSize; chunkSize = (remainingBytesToRead < chunkSize) ? remainingBytesToRead : chunkSize; if (remainingBytesToRead <= 0) { read = false; } } } while (read) { _smb2FileStore.ReadFile(out var chunk, fileHandle, start, chunkSize); stream.Write(chunk); start += chunkSize; if (chunk == null || chunk.Length < chunkSize) { read = false; } } stream.Position = 0; _smb2FileStore.CloseFile(fileHandle); } }
public async Task <NamedObjectBytes> GetFileByes(string uncPath, string contentFilename, long?startRange = null, long?endRange = null) { Stopwatch timer = Stopwatch.StartNew(); //Console.WriteLine($"SMB2ConnectionClient.GetFileBytes - CreateFile - PRECONNECT - THREADID:{this.GetHashCode()} NTStatus:{_status} FileStatus:NoConnection HOST:{_host} SHARE:{_share }PATH:{uncPath} FILENAME:{contentFilename} "); _smb2FileStore = TreeConnect(_share, out _status) as SMB2FileStore; if (_smb2FileStore == null) { _connected = false; return(null); } object fileHandle = new object(); FileStatus status; SMBLibrary.FileInformation fileInfo = null; var pathToFile = $"{uncPath}\\{contentFilename}"; _status = _smb2FileStore.CreateFile(out fileHandle, out status, pathToFile, AccessMask.GENERIC_READ, 0, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_NON_DIRECTORY_FILE, null); Console.WriteLine($"SMB2ConnectionClient.GetFileBytes - CreateFile- POSTCONNECT - THREADID:{this.GetHashCode()} NTStatus:{_status} FileStatus:{status} HOST:{_host} SHARE:{_share }PATH:{uncPath} FILENAME:{contentFilename} "); if (_status == NTStatus.STATUS_SUCCESS) { bool read = true; byte[] data = null; int start = (startRange != null && startRange.ToInt(0) >= 0) ? startRange.ToInt(0) : 0; int chunkSize = 65536; _smb2FileStore.GetFileInformation(out fileInfo, fileHandle, FileInformationClass.FileStandardInformation); //is this a ranged call? if (startRange != null && endRange != null) { chunkSize = ((endRange - startRange) < chunkSize) ? (int)(endRange - startRange + 1) : chunkSize; int remainingBytesToRead = endRange.ToInt(0) - startRange.ToInt(0) + 1; while (read) { _smb2FileStore.ReadFile(out var chunk, fileHandle, start, chunkSize); data = data == null ? chunk : data.Concat(chunk).ToArray(); start += chunkSize; remainingBytesToRead -= chunkSize; chunkSize = (remainingBytesToRead < chunkSize) ? remainingBytesToRead : chunkSize; if (remainingBytesToRead <= 0) { read = false; } } _smb2FileStore.CloseFile(fileHandle); return(new NamedObjectBytes() { FileBytes = data, TotalFileLength = ((FileStandardInformation)fileInfo).EndOfFile, RetrieveSpeedMs = timer.Elapsed.TotalMilliseconds }); } while (read) { _smb2FileStore.ReadFile(out var chunk, fileHandle, start, chunkSize); if (chunk == null) { Debugger.Break(); } data = data == null ? chunk : data.Concat(chunk).ToArray(); start += chunkSize; if (chunk == null || chunk.Length < chunkSize) { read = false; } } _smb2FileStore.CloseFile(fileHandle); return(new NamedObjectBytes() { FileBytes = data, TotalFileLength = ((FileStandardInformation)fileInfo).EndOfFile, RetrieveSpeedMs = timer.Elapsed.TotalMilliseconds }); } return(null); }