public static async Task <byte[]> ReadBytesAsync(this FPath path) { var mem = new SystemIO.MemoryStream(); using (var sr = path.Open(SystemIO.FileMode.Open, SystemIO.FileAccess.Read)) await sr.CopyToAsync(mem); return(mem.ToArray()); }
public Parler(ILogger log, BlobStores stores, GoogleCfg Cfg) { Log = log; this.Cfg = Cfg; Db = stores.Store(DataStoreType.DbStage); Dir = Path.GetTempPath().AsPath().Combine("recfluence", "parler"); }
public async Task Save(StringPath path, FPath file) { var blob = BlobRef(path); AutoPopulateProps(path, blob); await blob.UploadFromFileAsync(file.FullPath); }
public static ICollection <T> ReadFromCsv <T>(this FPath path, CsvConfiguration cfg = null) { cfg ??= DefaultConfig; using (var fs = path.OpenText()) { var csv = new CsvReader(fs, cfg); return(csv.GetRecords <T>().ToList()); } }
/// <summary>Returns an absolute path with the given one inside the app data folder with the given app name</summary> /// <param name="relativePath"></param> /// <param name="appName"></param> /// <returns></returns> public static FPath InAppData(this FPath relativePath, string appName) { if (relativePath.IsRooted) { throw new InvalidOperationException("The given path must be relative"); } return(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).AsPath().Combine(appName).Combine(relativePath)); }
public async Task Save(StringPath path, FPath file) { using (var stream = File.OpenRead(file.FullPath)) { var req = BlobUri(path).Put().WithStreamContent(stream).WithBlobHeaders(Storage); var res = await H.SendAsync(req); res.EnsureSuccessStatusCode(); } }
public static void WriteToCsv <T>(this IEnumerable <T> values, FPath path, CsvConfiguration cfg = null) { using (var fs = path.Open(FileMode.Create)) using (var tw = new StreamWriter(fs)) { cfg ??= new CsvConfiguration(CultureInfo.InvariantCulture); var csv = new CsvWriter(tw, cfg); csv.WriteRecords(values); } }
public FileCollection(ISimpleFileStore s3, Expression <Func <T, string> > getId, StringPath path, CollectionCacheType cacheType = CollectionCacheType.Memory, FPath localCacheDir = null) { Store = s3; GetId = getId.Compile(); Path = path; CacheType = cacheType; LocalCacheDir = localCacheDir; Cache = new KeyedCollection <string, T>(getId, theadSafe: true); }
public async Task Save(StringPath path, FPath file, ILogger log = null) { log ??= Log; var blob = BlobRef(path); AutoPopulateProps(path, blob); await blob.UploadFromFileAsync(file.FullPath); log.Debug("Saved {Path}", blob.Uri); }
/// <summary> /// Returns an absolute path with the given one inside the app data folder with the given app name /// </summary> /// <param name="relativePath"></param> /// <param name="appName"></param> /// <returns></returns> public static FPath InAppData(this FPath relativePath, string appName) { if (relativePath.IsRooted) { throw new InvalidOperationException("The given path must be relative"); } return(Environment.OSVersion.Platform.In(PlatformID.Unix) ? new FPath("~", appName).Combine(relativePath) : new FPath(Environment.GetEnvironmentVariable("LocalAppData")).Combine(appName).Combine(relativePath)); }
public static IEnumerable <FPath> EnumerateParents(this FPath path) { while (true) { var oldPath = path; path = path.Up(); if (path == oldPath) { break; } yield return(path); } }
/// <summary> /// Saves the result for the given query to Storage and a local tmp file /// </summary> async Task<FPath> SaveResult(IDbConnection db, FPath tempDir, ResQuery q) { var sw = Stopwatch.StartNew(); var reader = await ResQuery(db, q); var fileName = $"{q.Name}.csv.gz"; var tempFile = tempDir.Combine(fileName); using (var fileWriter = tempFile.Open(FileMode.Create, FileAccess.Write)) await reader.WriteCsvGz(fileWriter, fileName, Log); // save to both latest and the current date await SaveToLatestAndDateDirs(fileName, tempFile); Log.Information("Complete saving result {Name} in {Duration}", q.Name, sw.Elapsed); return tempFile; }
public static IEnumerable <FPath> EnumerateParents(this FPath path, bool includeIfDir) { if (path.IsDirectory && includeIfDir) { yield return(path); } while (true) { var oldPath = path; path = path.Up(); if (path == oldPath) { break; } yield return(path); } }
public static FPath Directories(this FPath p, Predicate <FPath> predicate, bool recursive, bool shortcutRecursion) { if (!recursive || !shortcutRecursion) { return(p.Directories(predicate, recursive)); } var result = new FPath(); var dirQueue = new Queue <FPath>(); dirQueue.Enqueue(p); while (dirQueue.Count > 0) { var dir = dirQueue.Dequeue(); foreach (var childDir in dir.Directories(predicate, false)) { dirQueue.Enqueue(childDir); result = result.Add(childDir); } } return(result); }
public static SystemIO.FileStream Open(this FPath path, SystemIO.FileMode mode, SystemIO.FileAccess access = SystemIO.FileAccess.ReadWrite, SystemIO.FileShare share = SystemIO.FileShare.None) => SystemIO.File.Open(path.ToString(), mode, access, share);
/// <summary> /// Finds the child directory of the given name within any of the parent directories (e.g. like node_modules /// resolution) /// </summary> /// <param name="path"></param> /// <param name="directoryName"></param> /// <returns></returns> public static IEnumerable <FPath> SubDirectoriesOfParent(this FPath path, string directoryName) => path.EnumerateParents().Select(p => p.Directories(directoryName)).Where(p => !p.IsEmtpy());
/// <summary> /// Finds the first parent that satisfies the predicate /// </summary> public static FPath Parent(this FPath path, Predicate <FPath> predicate) => path.EnumerateParents().FirstOrDefault(p => predicate(p));
public static FPath ParentWithFile(this FPath path, string filePattern) => path.EnumerateParents().FirstOrDefault(p => p.Files(filePattern, false).HasValue());
public static FPath DirOfParent(this FPath path, string searchPattern) => path.EnumerateParents().Select(p => p.Directories(searchPattern, false)).FirstOrDefault(d => !d.IsEmtpy());
static FPath Directories(this FPath p, string searchPattern) => p.Directories(searchPattern, false);
public async Task Save(StringPath path, FPath file, ILogger log = default) { var tu = new TransferUtility(S3); await tu.UploadAsync(file.FullPath, Cfg.Bucket, BasePath.Add(path)); }
public static FPath Files(this FPath p, string searchPattern) => p.Files(searchPattern, false);
public static FPath PathWithoutExtension(this FPath pathToMsql) => pathToMsql.Parent().Combine(pathToMsql.FileNameWithoutExtension);
public static async Task <string> ReadTextAsync(this FPath path) { using (var sr = path.OpenText()) return(await sr.ReadToEndAsync()); }
public static bool HasValue(this FPath path) => !IsEmtpy(path);
public static bool IsEmtpy(this FPath path) => path == null || path.ToStringArray().Length == 0;
/// <summary> /// Finds the first parent with the given directory name /// </summary> public static FPath Parent(this FPath path, string directoryName) => path.Parent(p => p.Tokens.Last() == directoryName);
public static FPath FileOfParent(this FPath path, string searchPattern) => path.EnumerateParents().Select(p => p.Files(searchPattern, false)).FirstOrDefault(f => !f.IsEmtpy());
public static SystemIO.StreamReader OpenText(this FPath path) => SystemIO.File.OpenText(path.FullPath);
public static SystemIO.FileInfo FileInfo(this FPath path) => new SystemIO.FileInfo(path.FullPath);