Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

feat: support UnixFSv1.5 metadata #1186

Merged
merged 13 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"explain-error": "^1.0.4",
"form-data": "^3.0.0",
"ipfs-block": "~0.8.1",
"ipfs-utils": "^0.4.0",
"ipfs-utils": "^0.5.0",
"ipld-dag-cbor": "~0.15.0",
"ipld-dag-pb": "^0.18.1",
"ipld-raw": "^4.0.0",
Expand Down
18 changes: 16 additions & 2 deletions src/add/form-data.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ exports.toFormData = async input => {
let i = 0

for await (const file of files) {
const headers = {}

if (file.mtime) {
headers.mtime = file.mtime
}

if (file.mode) {
headers.mode = file.mode
achingbrain marked this conversation as resolved.
Show resolved Hide resolved
}

if (file.content) {
// In the browser there's _currently_ no streaming upload, buffer up our
// async iterator chunks and append a big Blob :(
Expand All @@ -18,9 +28,13 @@ exports.toFormData = async input => {
bufs.push(chunk)
}

formData.append(`file-${i}`, new Blob(bufs, { type: 'application/octet-stream' }), encodeURIComponent(file.path))
formData.append(`file-${i}`, new Blob(bufs, { type: 'application/octet-stream' }), encodeURIComponent(file.path), {
header: headers
})
} else {
formData.append(`dir-${i}`, new Blob([], { type: 'application/x-directory' }), encodeURIComponent(file.path))
formData.append(`dir-${i}`, new Blob([], { type: 'application/x-directory' }), encodeURIComponent(file.path), {
header: headers
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is working...

const fd = new FormData
fd.append('test', new Blob(['data'], { type: 'application/octet-stream' }), 'path', { header: { mtime: Date.now(), mode: 'rwx' } })

const opts =  { method: 'post', body: fd }
const r0 = new Request('/test', opts)

await r0.text()
"-----------------------------21291924801818001399752183477

Content-Disposition: form-data; name=\"test\"; filename=\"path\"

Content-Type: application/octet-stream



data

-----------------------------21291924801818001399752183477--

"

}

i++
Expand Down
16 changes: 14 additions & 2 deletions src/add/form-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ exports.toFormData = async input => {
let i = 0

for await (const file of files) {
const headers = {}

if (file.mtime) {
headers.mtime = file.mtime
}

if (file.mode) {
headers.mode = file.mode.toString(8).padStart(4, '0')
}

if (file.content) {
// In Node.js, FormData can be passed a stream so no need to buffer
formData.append(
Expand All @@ -26,13 +36,15 @@ exports.toFormData = async input => {
{
filepath: encodeURIComponent(file.path),
contentType: 'application/octet-stream',
knownLength: file.content.length // Send Content-Length header if known
knownLength: file.content.length, // Send Content-Length header if known
header: headers
}
)
} else {
formData.append(`dir-${i}`, Buffer.alloc(0), {
filepath: encodeURIComponent(file.path),
contentType: 'application/x-directory'
contentType: 'application/x-directory',
header: headers
})
}

Expand Down
10 changes: 8 additions & 2 deletions src/add/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ module.exports = configure(({ ky }) => {
}
})

function toCoreInterface ({ name, hash, size }) {
return { path: name, hash, size: parseInt(size) }
function toCoreInterface ({ name, hash, size, mode, mtime }) {
return {
path: name,
hash,
size: parseInt(size),
mode: mode,
mtime: mtime
}
}
24 changes: 24 additions & 0 deletions src/files/chmod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const configure = require('../lib/configure')

module.exports = configure(({ ky }) => {
return function chmod (path, mode, options) {
options = options || {}

const searchParams = new URLSearchParams(options.searchParams)
searchParams.append('arg', path)
searchParams.append('mode', mode)
if (options.format) searchParams.set('format', options.format)
if (options.flush != null) searchParams.set('flush', options.flush)
if (options.hashAlg) searchParams.set('hash', options.hashAlg)
if (options.parents != null) searchParams.set('parents', options.parents)

return ky.post('files/chmod', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).text()
}
})
29 changes: 29 additions & 0 deletions src/files/touch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const configure = require('../lib/configure')

module.exports = configure(({ ky }) => {
return function touch (path, mtime, options) {
options = options || {}

if (isNaN(mtime)) {
options = mtime
mtime = null
}

const searchParams = new URLSearchParams(options.searchParams)
searchParams.append('arg', path)
if (mtime) searchParams.set('mtime', mtime)
if (options.format) searchParams.set('format', options.format)
if (options.flush != null) searchParams.set('flush', options.flush)
if (options.hashAlg) searchParams.set('hash', options.hashAlg)
if (options.parents != null) searchParams.set('parents', options.parents)

return ky.post('files/touch', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).text()
}
})