-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(snekfinder): add open-storage-gateway backend
Add the OSGBackend that communicates with osg.snek.at to store files. For that most functionality from IPFSBackend is moved to the abstract Backend. The OSGBackend and the IPFSBackend are both subclasses of the Backend class and implement seperate upload methods.
- Loading branch information
Showing
6 changed files
with
122 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {Backend} from './backend' | ||
|
||
export class OSGBackend extends Backend { | ||
constructor(public indexKey: string = 'snek-finder-osg-backend') { | ||
super() | ||
this.indexKey = indexKey | ||
} | ||
|
||
async upload(file: File) { | ||
const url = 'https://osg.snek.at/storage' | ||
|
||
const formData = new FormData() | ||
formData.append('file', file) | ||
|
||
const resp = await fetch(url, { | ||
body: formData, | ||
method: 'POST' | ||
}) | ||
|
||
const json = await resp.json() | ||
|
||
return `${url}/${json.file_id}` | ||
} | ||
} | ||
|
||
export default new OSGBackend('snek-finder-osg-backend-root') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,56 @@ | ||
export abstract class Backend { | ||
abstract readIndex(): Promise<any> | ||
abstract writeIndex(index: object): Promise<void> | ||
public initBackendLink?: string | ||
public onBackendLinkChange!: (link: string) => void | ||
|
||
public abstract indexKey: string | ||
|
||
abstract upload(file: File): Promise<any> | ||
|
||
async init() { | ||
if (this.initBackendLink) { | ||
const response = await (await fetch(this.initBackendLink)).json() | ||
|
||
await this.writeIndex(response) | ||
} | ||
} | ||
|
||
async readIndex() { | ||
if (window) { | ||
const getIndexData = () => { | ||
const indexData = window.localStorage.getItem(this.indexKey) | ||
|
||
return indexData && JSON.parse(indexData) | ||
} | ||
|
||
let indexData = getIndexData() | ||
|
||
if (!indexData) { | ||
await this.init() | ||
indexData = getIndexData() | ||
} | ||
|
||
return {data: indexData} | ||
} else { | ||
throw new Error( | ||
'window not defined, make sure to load this script in the browser' | ||
) | ||
} | ||
} | ||
|
||
async writeIndex(index: object) { | ||
if (window) { | ||
// make a file from index including date in name | ||
const indexData = JSON.stringify(index) | ||
const indexFile = new File([indexData], `${Date.now()}.json`) | ||
const indexUrl = await this.upload(indexFile) | ||
|
||
this.onBackendLinkChange(indexUrl) | ||
|
||
window.localStorage.setItem(this.indexKey, indexData) | ||
} else { | ||
throw new Error( | ||
'window not defined, make sure to load this script in the browser' | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters