Skip to content
This repository has been archived by the owner on Dec 24, 2022. It is now read-only.

Commit

Permalink
Add sessionStorage repository of media URLs GH-35
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Evans <[email protected]>
  • Loading branch information
mrbrianevans committed Jan 20, 2022
1 parent 5b915ea commit 32a4b09
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
10 changes: 7 additions & 3 deletions client/src/components/FileUploader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import FilePondPluginImagePreview from 'filepond-plugin-image-preview'
import ProcessingWorker from '../workers/processingWorker?worker'
import type { PostProcessedOutput } from '../../../lib/typedefs/PostProcess'
import { onlyFilename } from '../../../lib/common/PathProcessing'
// Register the plugins
registerPlugin(FilePondPluginImagePreview)
// registerPlugin(FilePondPluginImagePreview)
// a reference to the component, used to call FilePond methods
let pond
Expand All @@ -20,13 +21,16 @@
const worker = new ProcessingWorker()
worker.postMessage(fileItem.file)
console.dir(fileItem)
const workerOutput: PostProcessedOutput = await new Promise(resolve => {
worker.onmessage = message => {
resolve(message.data)
}
})
// console.log('metadata:', workerOutput.metadata)
files = [...files, workerOutput]
console.log('metadata', workerOutput.metadata)
if(workerOutput.metadata.isMedia)
sessionStorage.setItem(onlyFilename(fileItem.relativePath || fileItem.filename), workerOutput.data.url)
else files = [...files, workerOutput]
}
function handleRemoveFile(err, fileItem) {
Expand Down
1 change: 1 addition & 0 deletions client/src/workers/processingWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ addEventListener('message', async (e: MessageEvent<File>) => {
fileType
})
postProcessedOutput.metadata.filename = filename
if (isMedia(fileType)) postProcessedOutput.metadata.isMedia = true
postMessage(postProcessedOutput)
})
28 changes: 28 additions & 0 deletions lib-testing/test/common/PathProcessing.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { normalisePath, onlyFilename } from '../../../lib/common/PathProcessing'
import * as Assert from 'assert-js'

describe('test path processing utils functions', function () {
it('should normalise a windows style file path', function () {
const path = `C:\\\\Users\\\\abc\\\\Documents\\\\file.txt`
const normalisedPath = normalisePath(path)
Assert.equal(normalisedPath, 'C:/Users/abc/Documents/file.txt')
})

it('should get the filename of a windows style path', function () {
const path = `C:\\Users\\abc\\Documents\\file.txt`
const normalisedPath = onlyFilename(path)
Assert.equal(normalisedPath, 'file.txt')
})

it('should get only the filename with a leading slash and directory', function () {
const path = `/photos/file.txt`
const normalisedPath = onlyFilename(path)
Assert.equal(normalisedPath, 'file.txt')
})

it('should get only the filename with a leading slash', function () {
const path = `/file.txt`
const normalisedPath = onlyFilename(path)
Assert.equal(normalisedPath, 'file.txt')
})
})
11 changes: 11 additions & 0 deletions lib/common/PathProcessing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function normalisePath(path: string) {
// replace seperators
path = path.replace(/\\+/g, '/')
path = path.startsWith('/') ? path.slice(1) : path
return path
}

export function onlyFilename(path: string) {
const normalPath = normalisePath(path)
return normalPath.split('/').slice(-1)[0] // .at(-1) not yet available
}
2 changes: 1 addition & 1 deletion lib/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"sourceMap": true,
"composite": true,
"rootDir": "./",
"lib": ["ES2020"],
"lib": ["ES2020", "ESNext.Array"],
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
Expand Down

0 comments on commit 32a4b09

Please sign in to comment.