Skip to content

Commit 3d8da7a

Browse files
feat(pinned): pinned files are available in the '\ipfs' folder.
1 parent 50f4db3 commit 3d8da7a

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

IpfsMount/IpfsDokan.cs

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,7 @@ public NtStatus CreateFile(string fileName, DokanNet.FileAccess access, FileShar
5555
var ipfsFileName = fileName.Replace(@"\", "/");
5656
try
5757
{
58-
var x = ipfs.DoCommand("file/ls", ipfsFileName);
59-
var r = JObject.Parse(x);
60-
var hash = (string) r["Arguments"][ipfsFileName];
61-
var o = (JObject) r["Objects"][hash];
62-
var file = new IpfsFile()
63-
{
64-
Hash = (string)o["Hash"],
65-
Size = (long)o["Size"],
66-
IsDirectory = (string)o["Type"] == "Directory",
67-
Links = new List<IpfsFileLink>(0)
68-
};
69-
var links = o["Links"] as JArray;
70-
if (links != null)
71-
{
72-
file.Links = links
73-
.Select(l => new IpfsFileLink()
74-
{
75-
Name = (string)l["Name"],
76-
Hash = (string)l["Hash"],
77-
Size = (long)l["Size"],
78-
IsDirectory = (string)l["Type"] == "Directory",
79-
})
80-
.ToList();
81-
}
58+
var file = GetIpfsFile(ipfsFileName);
8259
info.Context = file;
8360
info.IsDirectory = file.IsDirectory;
8461
}
@@ -90,6 +67,35 @@ public NtStatus CreateFile(string fileName, DokanNet.FileAccess access, FileShar
9067
return DokanResult.Success;
9168
}
9269

70+
IpfsFile GetIpfsFile(string name)
71+
{
72+
var x = ipfs.DoCommand("file/ls", name);
73+
var r = JObject.Parse(x);
74+
var hash = (string)r["Arguments"][name];
75+
var o = (JObject)r["Objects"][hash];
76+
var file = new IpfsFile()
77+
{
78+
Hash = (string)o["Hash"],
79+
Size = (long)o["Size"],
80+
IsDirectory = (string)o["Type"] == "Directory",
81+
Links = new List<IpfsFileLink>(0)
82+
};
83+
var links = o["Links"] as JArray;
84+
if (links != null)
85+
{
86+
file.Links = links
87+
.Select(l => new IpfsFileLink()
88+
{
89+
Name = (string)l["Name"],
90+
Hash = (string)l["Hash"],
91+
Size = (long)l["Size"],
92+
IsDirectory = (string)l["Type"] == "Directory",
93+
})
94+
.ToList();
95+
}
96+
97+
return file;
98+
}
9399
public NtStatus GetFileInformation(string fileName, out FileInformation fileInfo, DokanFileInfo info)
94100
{
95101
fileInfo = new FileInformation {
@@ -278,6 +284,23 @@ public NtStatus FindFiles(string fileName, out IList<FileInformation> files, Dok
278284
return DokanResult.Success;
279285
}
280286

287+
// '/ipfs' contains the pinned files.
288+
if (fileName == @"\ipfs")
289+
{
290+
files = ipfs
291+
.PinnedObjects
292+
.Select(pin => GetIpfsFile(pin.Id))
293+
.Select(pinnedFile => new FileInformation
294+
{
295+
FileName = pinnedFile.Hash,
296+
Length = pinnedFile.Size,
297+
Attributes = FileAttributes.ReadOnly
298+
| (pinnedFile.IsDirectory ? FileAttributes.Directory : FileAttributes.Normal)
299+
})
300+
.ToList();
301+
return DokanResult.Success;
302+
}
303+
281304
// The root consists only of the root folders.
282305
if (fileName == rootName)
283306
{
@@ -300,7 +323,7 @@ public NtStatus FindFiles(string fileName, out IList<FileInformation> files, Dok
300323
}
301324

302325
files = new FileInformation[0];
303-
return DokanResult.NotImplemented;
326+
return DokanResult.Success;
304327
}
305328

306329
public NtStatus FindFilesWithPattern(string fileName, string searchPattern, out IList<FileInformation> files, DokanFileInfo info)

IpfsMountTests/IpfsDirectoryTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ public void Exists()
2020
}
2121

2222
[TestMethod]
23-
public void Has_No_Files()
23+
public void Has_Pinned_Files()
2424
{
2525
var info = new DirectoryInfo("t:/ipfs");
2626

2727
Assert.IsTrue(info.Exists);
28-
Assert.AreEqual(0, info.GetFileSystemInfos("*.*").Length);
28+
Assert.AreNotEqual(0, info.GetFileSystemInfos("*.*").Length);
2929
}
3030

3131
}

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
Mount the [InterPlanetary File System](https://ipfs.io/) as a mapped drive on Windows.
77

8+
## Features
9+
10+
- Mounts IPFS and IPNS on a drive letter, `ipfs-mount z:`
11+
- Unmounts the drive, `ipfs-mount z: /u`
12+
- [Pinned files](https://github.com/ipfs/examples/blob/master/examples/pinning/readme.md) are available in `z:\ipfs`
13+
814
## Installation
915

1016
[IPFS](https://ipfs.io/) and [Dokany](https://dokan-dev.github.io/) are required. If not already installed, they can be installed with [choco](https://chocolatey.org/).

0 commit comments

Comments
 (0)