fs-extra#createReadStream TypeScript Examples
The following examples show how to use
fs-extra#createReadStream.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UploadResourceUtil.ts From joplin-utils with MIT License | 6 votes |
static async uploadImageByPath(filePath: string) {
const param = {
title: path.parse(filePath).name,
data: createReadStream(path.resolve(filePath)),
}
console.log('uploadImageFromExplorer begin: ', filePath, param.title)
const res = await resourceApi.create(param)
const markdownLink = `![${param.title}](:/${res.id})`
console.log('uploadImageFromExplorer end: ', markdownLink)
return { res, markdownLink }
}
Example #2
Source File: UploadResourceUtil.ts From joplin-utils with MIT License | 6 votes |
static async uploadFileByPath(filePath: string) {
const param = {
title: path.basename(filePath),
data: createReadStream(path.resolve(filePath)),
}
console.log('uploadFileFromExplorer begin: ', filePath, param.title)
const res = await resourceApi.create(param)
const markdownLink = `[${res.title}](:/${res.id})`
console.log('uploadFileFromExplorer end: ', markdownLink)
return { res, markdownLink }
}
Example #3
Source File: JoplinNoteCommandService.ts From joplin-utils with MIT License | 5 votes |
async init(appConfig: AppConfig) {
if (!appConfig.token) {
return
}
config.token = appConfig.token
config.baseUrl = appConfig.baseUrl
setInterval(async () => {
await this.config.noteViewProvider.refresh()
}, 1000 * 10)
const tempNoteDirPath = path.resolve(GlobalContext.context.globalStorageUri.fsPath, '.tempNote')
const tempResourceDirPath = path.resolve(GlobalContext.context.globalStorageUri.fsPath, '.tempResource')
await AsyncArray.forEach([tempNoteDirPath, tempResourceDirPath], async (path) => {
// await remove(path)
await mkdirp(path)
})
watch(tempNoteDirPath)
.on('change', async (filePath) => {
const id = GlobalContext.openNoteMap.get(filePath)
if (!id) {
return
}
const content = await readFile(filePath, 'utf-8')
const { title, body } = JoplinNoteUtil.splitTitleBody(content)
const newNote = { id, body } as NoteProperties
if (title) {
newNote.title = title.startsWith('# ') ? title.substring(2) : title
}
await noteApi.update(newNote)
this.config.noteViewProvider.fire()
const resourceList = await noteApi.resourcesById(id)
GlobalContext.openNoteResourceMap.set(id, resourceList)
})
.on('error', (err) => {
logger.error('watch note error', err)
})
watch(tempResourceDirPath)
.on('change', async (filePath) => {
const id = GlobalContext.openResourceMap.get(filePath)
if (!id) {
return
}
await resourceApi.update({
id,
// title: path.basename(filePath),
data: createReadStream(filePath),
})
})
.on('error', (err) => {
logger.error('watch resource error', err)
})
}
Example #4
Source File: CreateTestResource.ts From joplin-utils with MIT License | 5 votes |
export async function createTestResource() {
const title = 'image title'
const path = resolve(__dirname, '../resource/resourcesByFileId.png')
return await resourceApi.create({
title,
data: createReadStream(path),
})
}
Example #5
Source File: ResourceApi.test.ts From joplin-utils with MIT License | 4 votes |
describe('test ResourceApi', () => {
let id: string
beforeAll(async () => {
id = (await createTestResource()).id
})
afterAll(async () => {
await resourceApi.remove(id)
})
const tempPath = path.resolve(__dirname, '.temp')
beforeEach(async () => {
await remove(tempPath)
await mkdirp(tempPath)
})
it('test list', async () => {
const res = await resourceApi.list({ fields: ['id', 'title'] })
console.log(res)
expect(res.items.length).toBeGreaterThan(0)
})
it('test get', async () => {
const res = await resourceApi.get(id)
console.log(res)
expect(res.id).toBe(id)
})
/**
* TODO 一个官方未修复的 bug,参考:https://github.com/laurent22/joplin/issues/4575
*/
it.skip('test get filename', async () => {
const res = await resourceApi.get(id, ['id', 'filename'])
console.log(res)
expect(res.filename).not.toBe('')
})
describe('diff fetch and axios', () => {
const path = resolve(__dirname, '../resource/resourcesByFileId.png')
it('test create', async () => {
const title = 'image title'
const json = await resourceApi.create({
title,
data: createReadStream(path),
})
expect(json.title).toBe(title)
})
})
describe('test update', () => {
it('basic example', async () => {
const title = `new title ${Date.now()}`
const updateRes = await resourceApi.update({ id, title })
expect(updateRes.title).toBe(title)
})
it('update file', async () => {
const content = 'test'
const txtPath = path.resolve(tempPath, 'test.txt')
await writeFile(txtPath, content)
await resourceApi.update({ id, data: createReadStream(txtPath) })
const res = await resourceApi.fileByResourceId(id)
expect(res.toString()).toBe(content)
})
it('update properties and file', async () => {
const title = `new title ${Date.now()}`
const content = 'test'
const txtPath = path.resolve(tempPath, 'test.txt')
await writeFile(txtPath, content)
const updateRes = await resourceApi.update({ id, title, data: createReadStream(txtPath) })
expect(updateRes.title).toBe(title)
const res = await resourceApi.fileByResourceId(id)
expect(res.toString()).toBe(content)
})
it('update file only', async () => {
const content = 'test'
const txtPath = path.resolve(tempPath, 'test.txt')
await writeFile(txtPath, content)
const { title } = await resourceApi.get(id)
await resourceApi.update({ id, data: createReadStream(txtPath) })
const res = await resourceApi.fileByResourceId(id)
expect(res.toString()).toBe(content)
expect((await resourceApi.get(id)).title).toBe(title)
})
})
/**
* 已知错误
* https://discourse.joplinapp.org/t/pre-release-v2-8-is-now-available-updated-14-april/25158/10?u=rxliuli
*/
it.skip('test remove ', async () => {
const id = (await createTestResource()).id
await resourceApi.remove(id)
await expect(resourceApi.get(id)).rejects.toThrowError()
})
it('test fileByResourceId', async () => {
const res = await resourceApi.fileByResourceId(id)
console.log(typeof res)
const path = resolve(tempPath, 'resourcesByFileId.png')
await writeFile(path, res)
expect(pathExistsSync(path)).toBeTruthy()
})
it('test to get the size of the attachment resource', async () => {
const id = (await createTestResource()).id
const res = await resourceApi.get(id, ['id', 'title', 'size'])
const stats = await stat(path.resolve(__dirname, '../resource/resourcesByFileId.png'))
expect(res.size).toEqual(stats.size)
})
})