/// <summary> /// Write a blob to the object index. /// </summary> /// <param name="blob">Blob to write.</param> /// <returns>Hashed blob.</returns> public static Span <byte> Write(Span <byte> blob) { var hash = Hash.HashObject(blob); var blobPath = NitPath.GetFullObjectPath(hash); // ensure folders exist var blobFolder = NitPath.GetObjectDirectoryPath(hash); Directory.CreateDirectory(blobFolder); File.WriteAllBytes(blobPath, blob.ToArray()); return(hash); }
/// <summary> /// Copy source file to object index. /// </summary> /// <param name="targetPath">Path to write blob.</param> /// <returns>Hash of blob written.</returns> public static Span <byte> Write(string targetPath) { if (!File.Exists(targetPath)) { throw new ArgumentException("Target does not exist."); } var hash = Hash.HashFile(targetPath); var fullPath = NitPath.GetFullObjectPath(hash); var dirPath = NitPath.GetObjectDirectoryPath(hash); if (File.Exists(fullPath)) { // already exists, don't bother return(hash); } // ensure the folders exist Directory.CreateDirectory(dirPath); // copy the file, using the hex hash as the filename File.Copy(targetPath, fullPath); return(hash); }