|
1 | 1 | import { logger } from '@libp2p/logger'
|
2 |
| -import { exporter } from 'ipfs-unixfs-exporter' |
3 |
| -import { DoesNotExistError, InvalidParametersError } from '../../errors.js' |
| 2 | +import { walkPath } from 'ipfs-unixfs-exporter' |
| 3 | +import all from 'it-all' |
| 4 | +import { DoesNotExistError } from '../../errors.js' |
4 | 5 | import { addLink } from './add-link.js'
|
5 | 6 | import { cidToDirectory } from './cid-to-directory.js'
|
6 | 7 | import { cidToPBLink } from './cid-to-pblink.js'
|
@@ -37,57 +38,17 @@ export async function resolve (cid: CID, path: string | undefined, blockstore: B
|
37 | 38 | return { cid }
|
38 | 39 | }
|
39 | 40 |
|
40 |
| - log('resolve "%s" under %c', path, cid) |
41 |
| - |
42 |
| - const parts = path.split('/').filter(Boolean) |
43 |
| - const segments: Segment[] = [{ |
44 |
| - name: '', |
45 |
| - cid, |
46 |
| - size: 0n |
47 |
| - }] |
48 |
| - |
49 |
| - for (let i = 0; i < parts.length; i++) { |
50 |
| - const part = parts[i] |
51 |
| - const result = await exporter(cid, blockstore, options) |
52 |
| - |
53 |
| - log('resolving "%s"', part, result) |
54 |
| - |
55 |
| - if (result.type === 'file') { |
56 |
| - if (i < parts.length - 1) { |
57 |
| - throw new InvalidParametersError('Path was invalid') |
58 |
| - } |
59 |
| - |
60 |
| - cid = result.cid |
61 |
| - } else if (result.type === 'directory') { |
62 |
| - let dirCid: CID | undefined |
63 |
| - |
64 |
| - for await (const entry of result.content()) { |
65 |
| - if (entry.name === part) { |
66 |
| - dirCid = entry.cid |
67 |
| - break |
68 |
| - } |
69 |
| - } |
70 |
| - |
71 |
| - if (dirCid == null) { |
72 |
| - throw new DoesNotExistError('Could not find path in directory') |
73 |
| - } |
74 |
| - |
75 |
| - cid = dirCid |
76 |
| - |
77 |
| - segments.push({ |
78 |
| - name: part, |
79 |
| - cid, |
80 |
| - size: result.size |
81 |
| - }) |
82 |
| - } else { |
83 |
| - throw new InvalidParametersError('Could not resolve path') |
84 |
| - } |
| 41 | + const p = `/ipfs/${cid}${path == null ? '' : `/${path}`}` |
| 42 | + const segments = await all(walkPath(p, blockstore, options)) |
| 43 | + |
| 44 | + if (segments.length === 0) { |
| 45 | + throw new DoesNotExistError('Could not find path in directory') |
85 | 46 | }
|
86 | 47 |
|
87 | 48 | log('resolved %s to %c', path, cid)
|
88 | 49 |
|
89 | 50 | return {
|
90 |
| - cid, |
| 51 | + cid: segments[segments.length - 1].cid, |
91 | 52 | path,
|
92 | 53 | segments
|
93 | 54 | }
|
|
0 commit comments