public static string ToMD5(this SPath path) { byte[] computeHash; using (var md5 = MD5.Create()) { using (var stream = SPath.FileSystem.OpenRead(path.MakeAbsolute())) { computeHash = md5.ComputeHash(stream); } } return(BitConverter.ToString(computeHash).Replace("-", string.Empty).ToLower()); }
public static ITask UpdateIndexFromFilesInFolderTask(SPath folderWithFiles, SPath indexFileToGenerate, SPath?indexTemplate = null) { Index?templateIndex = null; if (indexTemplate.HasValue && indexTemplate.Value.FileExists()) { templateIndex = Index.Load(indexTemplate); } folderWithFiles = folderWithFiles.MakeAbsolute(); var dt = DateTimeOffset.Now.Date.ToString("yyyyMMdd"); TaskQueue <Asset> hashTask = new TaskQueue <Asset>(TaskManager) { Message = "Calculating hashes..." }; foreach (var file in folderWithFiles.Files()) { Asset asset = default; if ((templateIndex?.Assets.Any(x => x.Filename == file.FileName)) ?? false) { var templateAsset = templateIndex.Value.Assets.First(x => x.Filename == file.FileName); asset.LocalPath = file; asset.Path = templateAsset.Path; asset.Url = templateAsset.Url.Combine("assets") .Combine(dt) .Combine(file.FileName); asset.NeedsUnzip = templateAsset.NeedsUnzip; } else { asset.LocalPath = file; asset.Path = file.FileNameWithoutExtension.ToSPath(); asset.NeedsUnzip = file.ExtensionWithDot == ".zip" ? true : false; asset.Url = $"{DefaultWebServerUrl}:{DefaultWebServerPort}/assets/{dt}/{file.FileName}"; } hashTask.Queue(new FuncTask <Asset, Asset>(TaskManager, (_, x) => { x.Hash = x.LocalPath.ToMD5(); x.LocalPath = default; return(x); }, () => asset) { Message = file.FileName });