Skip to content

Commit 440d7e9

Browse files
first commit
1 parent 8b284d0 commit 440d7e9

File tree

10 files changed

+1155
-70
lines changed

10 files changed

+1155
-70
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ npm-debug.log*
2929
/platforms
3030
/plugins
3131
/www
32+
src/hooks/.env.firebase.ts

package-lock.json

Lines changed: 787 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@ionic/vue": "5.4.0-dev.202009032307.949031e",
1919
"@ionic/vue-router": "5.4.0-dev.202009032307.949031e",
2020
"core-js": "^3.6.5",
21+
"firebase": "^7.19.1",
2122
"vue": "^3.0.0-0",
2223
"vue-router": "^4.0.0-0"
2324
},

src/hooks/firebase-file-upload.ts

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { toRefs, reactive, ref } from "vue";
2+
import firebase from "firebase";
3+
// Required for side-effects
4+
import "firebase/firestore";
5+
6+
import FIREBASE_CONFIG from "./.env.firebase";
7+
8+
// initialize firebase, this is directly from the firebase documentation
9+
// regarding getting started for the web
10+
if (firebase.apps.length === 0) {
11+
firebase.initializeApp(FIREBASE_CONFIG);
12+
}
13+
14+
const STORAGE_FILE_PATH = "image-capture/";
15+
16+
export default function() {
17+
const progress = ref(0);
18+
const files = ref<any>([]);
19+
const state = reactive<{
20+
error: any | null;
21+
loading: boolean;
22+
resultData: any | null;
23+
}>({
24+
// error if one happens
25+
error: null,
26+
// if the query is loading or not
27+
loading: false,
28+
// result from upload
29+
resultData: {},
30+
});
31+
32+
// get the database storage
33+
const storageRef = firebase.storage().ref();
34+
35+
const listFiles = () => {
36+
files.value = [];
37+
return firebase
38+
.storage()
39+
.ref("image-capture/")
40+
.listAll()
41+
.then((r: firebase.storage.ListResult) => {
42+
console.log(r);
43+
r.items.forEach(async (e: firebase.storage.Reference) => {
44+
console.log(e.fullPath);
45+
const url = await e.getDownloadURL()
46+
files.value.push({name : e.fullPath, url});
47+
});
48+
49+
return files.value;
50+
});
51+
};
52+
53+
(async ()=>{
54+
await listFiles()
55+
})();
56+
57+
const uploadData = async (fileData: string, name?: string) => {
58+
// initialize upload information
59+
state.error = null;
60+
state.loading = true;
61+
62+
progress.value = 0;
63+
// ensure unique file names
64+
const uniquePathName = new Date().getTime() + "-" + name;
65+
const ext = name?.split(".")[1].toLowerCase();
66+
67+
try {
68+
const ref = storageRef.child(
69+
`${STORAGE_FILE_PATH}\\${uniquePathName}.${ext}`
70+
);
71+
const uploadTask = ref.putString(fileData, "data_url", {
72+
contentType: "image/jpeg",
73+
});
74+
75+
// The first example.
76+
uploadTask.on(
77+
firebase.storage.TaskEvent.STATE_CHANGED,
78+
(_progress) => {
79+
const prog = _progress.bytesTransferred / _progress.totalBytes;
80+
progress.value = prog;
81+
console.log("STATE_CHANGED", prog);
82+
},
83+
(_error) => {
84+
state.error = _error;
85+
state.loading = false;
86+
console.log("ERROR", _error);
87+
},
88+
// eslint-disable-next-line no-unused-vars
89+
async () => {
90+
state.error = null;
91+
state.loading = false;
92+
93+
const downloadUrl = await uploadTask.snapshot.ref.getDownloadURL();
94+
state.resultData = {
95+
downloadUrl,
96+
name: uploadTask.snapshot.metadata.name,
97+
image: {
98+
ref: uploadTask.snapshot.ref.fullPath,
99+
size: uploadTask.snapshot.metadata.size,
100+
contentType: uploadTask.snapshot.metadata.contentType,
101+
timeCreated: uploadTask.snapshot.metadata.timeCreated,
102+
},
103+
};
104+
105+
// set progress back to zero when complete
106+
progress.value = 0;
107+
}
108+
);
109+
} catch (_error) {
110+
state.loading = false;
111+
state.error = _error;
112+
progress.value = 0;
113+
}
114+
};
115+
return {
116+
...toRefs(state),
117+
progress,
118+
uploadData,
119+
listFiles,
120+
files
121+
};
122+
}

src/router/index.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
11
import { createRouter, createWebHistory } from '@ionic/vue-router';
22
import { RouteRecordRaw } from 'vue-router';
3-
import Home from '../views/Home.vue'
4-
import Geolocation from '../views/Geolocation.vue'
3+
import Tabs from "../views/Tabs.vue";
4+
55

66
const routes: Array<RouteRecordRaw> = [
77
{
8-
path: '/',
9-
redirect: '/home'
8+
path: "",
9+
redirect: '/tabs/list'
1010
},
1111
{
12-
path: '/geolocation',
13-
name: 'Geolocation',
14-
component: Geolocation
12+
path: "/image-detail/:url",
13+
component: () => import("@/views/ImageDetail.vue"),
1514
},
1615
{
17-
path: '/home',
18-
name: 'Home',
19-
component: Home
16+
path: "/tabs/",
17+
component: Tabs,
18+
children: [
19+
{
20+
path: "",
21+
redirect: "list",
22+
},
23+
{
24+
path: "upload",
25+
component: () => import("@/views/Home.vue"),
26+
},
27+
{
28+
path: "list",
29+
component: () => import("@/views/AllUploads.vue"),
30+
},
31+
{
32+
path: "geolocation",
33+
component: () => import("@/views/Geolocation.vue"),
34+
},
35+
],
2036
}
21-
]
37+
];
38+
2239

2340
const router = createRouter({
2441
history: createWebHistory(process.env.BASE_URL),

src/views/AllUploads.vue

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<template>
2+
<ion-page>
3+
<ion-header :translucent="true">
4+
<ion-toolbar>
5+
<ion-title>Uploaded Files</ion-title>
6+
</ion-toolbar>
7+
</ion-header>
8+
9+
<ion-content :fullscreen="true" class="ion-padding">
10+
<h2>All Uploads</h2>
11+
<ion-refresher slot="fixed" @ionRefresh="doRefresh($event)">
12+
<ion-refresher-content></ion-refresher-content>
13+
</ion-refresher>
14+
<ion-item v-for="(item, index) in files" :key="index" style="--padding-start:0px">
15+
<ion-label class="ion-text-wrap">
16+
<div>{{item.name.split("\\")[1] }}</div>
17+
<!-- <a :href="item.url">view</a> -->
18+
<ion-button @click="router.push('/image-detail/' +encodeURIComponent(item.url))">VIEW ITEM</ion-button>
19+
</ion-label>
20+
</ion-item>
21+
</ion-content>
22+
</ion-page>
23+
</template>
24+
25+
<script lang="ts">
26+
import {
27+
IonContent,
28+
IonHeader,
29+
IonPage,
30+
IonTitle,
31+
IonToolbar,
32+
IonLabel,
33+
IonItem,
34+
IonRefresher,
35+
IonRefresherContent,
36+
} from "@ionic/vue";
37+
import { defineComponent } from "vue";
38+
import useFirebaseFileUpload from "../hooks/firebase-file-upload";
39+
import { useRouter } from 'vue-router';
40+
41+
export default defineComponent({
42+
name: "AllUploads",
43+
components: {
44+
IonContent,
45+
IonHeader,
46+
IonPage,
47+
IonTitle,
48+
IonToolbar,
49+
IonLabel,
50+
IonItem,
51+
IonRefresher,
52+
IonRefresherContent,
53+
},
54+
setup() {
55+
const { files, listFiles } = useFirebaseFileUpload();
56+
const router = useRouter();
57+
58+
const doRefresh = async (event: any) => {
59+
await listFiles();
60+
console.log("complete..");
61+
event.target.complete();
62+
};
63+
return { files, doRefresh, router };
64+
},
65+
});
66+
</script>
67+
68+
<style scoped>
69+
</style>

src/views/Geolocation.vue

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
<ion-page>
33
<ion-header :translucent="true">
44
<ion-toolbar>
5-
<IonButtons>
6-
<ion-back-button />
7-
</IonButtons>
85
<ion-title>Geolocation</ion-title>
96
</ion-toolbar>
107
</ion-header>
@@ -25,9 +22,7 @@ import {
2522
IonPage,
2623
IonTitle,
2724
IonToolbar,
28-
IonButtons,
2925
IonButton,
30-
IonBackButton,
3126
} from "@ionic/vue";
3227
import { defineComponent, ref } from "vue";
3328
import { Plugins } from "@capacitor/core";
@@ -41,8 +36,6 @@ export default defineComponent({
4136
IonPage,
4237
IonTitle,
4338
IonToolbar,
44-
IonButtons,
45-
IonBackButton,
4639
IonButton,
4740
},
4841
setup() {

0 commit comments

Comments
 (0)