-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,20 +1,91 @@ | ||
import { ModelDatabase } from "./dexie-utils"; | ||
import { getApp } from "firebase/app"; | ||
import { getDownloadURL, getStorage, ref } from "firebase/storage"; | ||
import { | ||
FirebaseStorage, | ||
getDownloadURL, | ||
getStorage, | ||
ref, | ||
} from "firebase/storage"; | ||
import { Building } from "../../types"; | ||
|
||
// CORS problem solution: https://stackoverflow.com/a/58613527 | ||
export class BuildingDatabase { | ||
private db: ModelDatabase; | ||
|
||
constructor() { | ||
this.db = new ModelDatabase(); | ||
} | ||
|
||
async getModels(building: Building) { | ||
const appInstance = getApp(); | ||
const storageInstance = getStorage(appInstance); | ||
const instance = getStorage(appInstance); | ||
|
||
const urls: string[] = []; | ||
for (const model of building.models) { | ||
const fileRef = ref(storageInstance, model.id); | ||
const fileUrl = await getDownloadURL(fileRef); | ||
urls.push(fileUrl); | ||
const url = await this.getModelURL(instance, model.id); | ||
urls.push(url); | ||
} | ||
|
||
this.db.close(); | ||
|
||
return urls; | ||
} | ||
|
||
async clearCache(building: Building) { | ||
await this.db.open(); | ||
for (const model of building.models) { | ||
localStorage.removeItem(model.id); | ||
} | ||
await this.db.delete(); | ||
this.db = new ModelDatabase(); | ||
this.db.close(); | ||
} | ||
|
||
async deleteModel(id: string) { | ||
await this.db.open(); | ||
if (this.isModelCached(id)) { | ||
localStorage.removeItem(id); | ||
await this.db.models.where("id").equals(id).delete(); | ||
} | ||
this.db.close(); | ||
} | ||
|
||
private async getModelURL(instance: FirebaseStorage, id: string) { | ||
if (this.isModelCached(id)) { | ||
return this.getModelFromLocalCache(id); | ||
} else { | ||
return this.getModelFromFirebase(instance, id); | ||
} | ||
} | ||
|
||
private async getModelFromFirebase(instance: FirebaseStorage, id: string) { | ||
const fileRef = ref(instance, id); | ||
const url = await getDownloadURL(fileRef); | ||
await this.cacheModel(id, url); | ||
console.log("Got model from firebase!"); | ||
return url; | ||
} | ||
|
||
private async getModelFromLocalCache(id: string) { | ||
const found = await this.db.models.where("id").equals(id).toArray(); | ||
const file = found[0].file; | ||
console.log("Got model from local cache!"); | ||
return URL.createObjectURL(file); | ||
} | ||
|
||
private isModelCached(id: string) { | ||
const stored = localStorage.getItem(id); | ||
return stored !== null; | ||
} | ||
|
||
private async cacheModel(id: string, url: string) { | ||
const time = performance.now().toString(); | ||
localStorage.setItem(id, time); | ||
const rawData = await fetch(url); | ||
const file = await rawData.blob(); | ||
await this.db.models.add({ | ||
id, | ||
file, | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Dexie } from "dexie"; | ||
|
||
interface IModel { | ||
id: string; | ||
file: Blob; | ||
} | ||
|
||
export class ModelDatabase extends Dexie { | ||
models!: Dexie.Table<IModel, number>; | ||
|
||
constructor() { | ||
super("ModelDatabase"); | ||
this.version(2).stores({ | ||
models: "id, file", | ||
}); | ||
} | ||
} |
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