public override async Task <bool> MoveFile(FlatFile file, EFlatFilePath fromDirectory, EFlatFilePath toDirectory, string fileName) { try { var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); var fileNameExtension = Path.GetExtension(fileName); var version = 0; string newFileName; var cloudFileDirectory = await GetFileDirectory(file); var cloudFromDirectory = cloudFileDirectory.GetDirectoryReference(file.GetPath(fromDirectory)); var cloudToDirectory = cloudFileDirectory.GetDirectoryReference(file.GetPath(toDirectory)); CloudBlob sourceFile = cloudFromDirectory.GetBlockBlobReference(fileName); CloudBlob targetFile = cloudToDirectory.GetBlockBlobReference(fileName); while (await targetFile.ExistsAsync()) { version++; newFileName = fileNameWithoutExtension + "_" + version.ToString() + fileNameExtension; targetFile = cloudToDirectory.GetBlockBlobReference(newFileName); } await targetFile.StartCopyAsync(sourceFile.Uri); await sourceFile.DeleteAsync(); return(true); } catch (Exception ex) { throw new ConnectionException($"Failed to move file {file.Name} filename {Filename} from {fromDirectory} to {toDirectory}. {ex.Message}", ex); } }
public override string GetFullPath(FlatFile file, EFlatFilePath path) { var fullPath = Path.Combine(FilePath(), file.FileRootPath ?? "", file.GetPath(path)); ValidatePath(fullPath); return(fullPath); }
public override Task <bool> CreateDirectory(FlatFile file, EFlatFilePath path) { try { var directory = FilePath(); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } if (file != null && !string.IsNullOrEmpty(file.FileRootPath)) { directory = Path.Combine(FilePath(), file.FileRootPath); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } } if (file != null && path != EFlatFilePath.None) { directory = Path.Combine(directory, file.GetPath(path)); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } } return(Task.FromResult(true)); } catch (Exception ex) { throw new ConnectionException($"Error occurred creating directory {path}. {ex.Message}", ex); } }
public override async Task <bool> SaveFileStream(FlatFile file, EFlatFilePath path, string fileName, Stream fileStream) { try { var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); var fileNameExtension = Path.GetExtension(fileName); var version = 0; string newFileName; var cloudFileDirectory = await GetFileDirectory(file); var cloudSubDirectory = cloudFileDirectory.GetDirectoryReference(file.GetPath(path)); var cloudFile = cloudSubDirectory.GetBlockBlobReference(fileName); while (await cloudFile.ExistsAsync()) { version++; newFileName = fileNameWithoutExtension + "_" + version.ToString() + fileNameExtension; cloudFile = cloudSubDirectory.GetBlockBlobReference(newFileName); } await cloudFile.UploadFromStreamAsync(fileStream); fileStream.Dispose(); return(true); } catch (Exception ex) { throw new ConnectionException($"Failed save the file {file.Name} name {Filename} in path {path}. {ex.Message}", ex); } }
public override async Task <bool> CreateDirectory(FlatFile file, EFlatFilePath path) { try { var directory = await GetDatabaseDirectory(); var cloudFileDirectory = directory; if (file != null && !string.IsNullOrEmpty(file.FileRootPath)) { cloudFileDirectory = directory.GetDirectoryReference(file.FileRootPath); await cloudFileDirectory.Container.CreateIfNotExistsAsync(); } if (file != null && path != EFlatFilePath.None) { cloudFileDirectory = cloudFileDirectory.GetDirectoryReference(file.GetPath(path)); await cloudFileDirectory.Container.CreateIfNotExistsAsync(); } return(true); } catch (Exception ex) { throw new ConnectionException($"Failed to create the directory for file {file.Name} path {path}. {ex.Message}", ex); } }
public override async Task <Stream> GetWriteFileStream(FlatFile file, EFlatFilePath path, string fileName) { try { var cloudFileDirectory = await GetFileDirectory(file); var cloudSubDirectory = cloudFileDirectory.GetDirectoryReference(file.GetPath(path)); Stream stream = await cloudSubDirectory.GetBlockBlobReference(fileName).OpenWriteAsync(); return(stream); } catch (Exception ex) { throw new ConnectionException($"Failed write the file {file.Name} name {Filename} in path {path}. {ex.Message}", ex); } }
public override string GetFullPath(FlatFile file, EFlatFilePath path) { var fullPath = _workingDirectory + "/" + DefaultDatabase; if (!string.IsNullOrEmpty(file.FileRootPath)) { fullPath += "/" + file.FileRootPath; } var subPath = file.GetPath(path); if (!string.IsNullOrEmpty(subPath)) { fullPath += "/" + subPath; } return(fullPath); }
public override async Task <DexihFiles> GetFileEnumerator(FlatFile file, EFlatFilePath path, string searchPattern) { try { var files = new List <DexihFileProperties>(); var cloudFileDirectory = await GetFileDirectory(file); var pathstring = file.GetPath(path); var pathlength = pathstring.Length + 1; var cloudSubDirectory = cloudFileDirectory.GetDirectoryReference(pathstring); BlobContinuationToken continuationToken = null; var list = new List <IListBlobItem>(); do { var filesList = await cloudSubDirectory.ListBlobsSegmentedAsync(false, BlobListingDetails.None, 500, continuationToken, null, null); continuationToken = filesList.ContinuationToken; list.AddRange(filesList.Results); } while (continuationToken != null); foreach (CloudBlob cloudFile in list) { await cloudFile.FetchAttributesAsync(); var fileName = cloudFile.Name.Substring(pathlength); if (string.IsNullOrEmpty(searchPattern) || FitsMask(file.Name, searchPattern)) { files.Add(new DexihFileProperties() { FileName = fileName, LastModified = cloudFile.Properties.LastModified.Value.DateTime, Length = cloudFile.Properties.Length }); } } var newFiles = new DexihFiles(files.ToArray()); return(newFiles); } catch (Exception ex) { throw new ConnectionException($"Failed get file {file.Name} files in path {path} with pattern {searchPattern}. {ex.Message}", ex); } }
public override async Task <List <DexihFileProperties> > GetFileList(FlatFile file, EFlatFilePath path) { try { var files = new List <DexihFileProperties>(); var cloudFileDirectory = await GetFileDirectory(file); var pathstring = file.GetPath(path); var pathlength = pathstring.Length + 1; var cloudSubDirectory = cloudFileDirectory.GetDirectoryReference(pathstring); BlobContinuationToken continuationToken = null; var list = new List <IListBlobItem>(); do { var filesList = await cloudSubDirectory.ListBlobsSegmentedAsync(true, BlobListingDetails.None, 500, continuationToken, null, null); continuationToken = filesList.ContinuationToken; list.AddRange(filesList.Results); } while (continuationToken != null); foreach (CloudBlob cloudFile in list) { await cloudFile.FetchAttributesAsync(); var fileName = cloudFile.Name.Substring(pathlength); files.Add(new DexihFileProperties() { FileName = fileName, LastModified = cloudFile.Properties.LastModified.Value.DateTime, Length = cloudFile.Properties.Length, ContentType = cloudFile.Properties.ContentType }); } return(files); } catch (Exception ex) { throw new ConnectionException($"Failed get filelist {file.Name} files in path {path}. {ex.Message}", ex); } }
public override Task <bool> CreateDirectory(FlatFile file, EFlatFilePath path) { try { using (var client = GetSftpClient()) { var directory = DefaultDatabase; if (!client.Exists(directory)) { client.CreateDirectory(directory); } if (file != null && !string.IsNullOrEmpty(file.FileRootPath)) { directory = CombinePath(DefaultDatabase, file.FileRootPath); if (!client.Exists(directory)) { client.CreateDirectory(directory); } } if (file != null && path != EFlatFilePath.None) { directory = CombinePath(directory, file.GetPath(path)); if (!client.Exists(directory)) { client.CreateDirectory(directory); } } return(Task.FromResult(true)); } } catch (Exception ex) { throw new ConnectionException($"Error occurred creating directory {path}. {ex.Message}", ex); } }
public override async Task <bool> DeleteFile(FlatFile file, EFlatFilePath path, string fileName) { try { var cloudFileDirectory = await GetFileDirectory(file); var cloudSubDirectory = cloudFileDirectory.GetDirectoryReference(file.GetPath(path)); CloudBlob cloudFile = cloudSubDirectory.GetBlockBlobReference(fileName); await cloudFile.DeleteAsync(); return(true); } catch (Exception ex) { throw new ConnectionException($"Failed to delete file {file.Name} filename {Filename} from {path}. {ex.Message}", ex); } }