Skip to content

Commit

Permalink
feat(Post download): manage errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroAkbal committed Mar 18, 2022
1 parent 2e2be99 commit 52d3496
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions components/pages/posts/post/PostDownload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</template>

<script>
import { mapGetters } from 'vuex'
import { mapActions, mapGetters } from 'vuex'
import { DownloadIcon } from 'vue-feather-icons'
import { ProxyHelper } from "~/assets/js/ProxyHelper";
Expand All @@ -46,6 +46,10 @@ export default {
},
methods: {
...mapActions([
'errorManager'
]),
downloadBlobToDevice(blob, fileName) {
const BLOB_OBJECT_URL = URL.createObjectURL(blob)
Expand All @@ -63,8 +67,7 @@ export default {
URL.revokeObjectURL(BLOB_OBJECT_URL)
},
async downloadMedia() {
async fetchUrlIntoBlob() {
const PROXIED_MEDIA_URL = ProxyHelper.proxyUrl(this.mediaUrl);
const RESPONSE = await this.$axios.get(PROXIED_MEDIA_URL, {
Expand All @@ -74,11 +77,28 @@ export default {
const RESPONSE_BLOB = RESPONSE.data
const FILE_EXTENSION = RESPONSE.headers['content-type'].split('/')[1]
return { RESPONSE, RESPONSE_BLOB };
},
async downloadMedia() {
try {
const { RESPONSE, RESPONSE_BLOB } = await this.fetchUrlIntoBlob();
const FILE_EXTENSION = RESPONSE.headers['content-type'].split('/')[1]
const FILE_NAME = this.mediaName + '.' + FILE_EXTENSION
this.downloadBlobToDevice(RESPONSE_BLOB, FILE_NAME);
const FILE_NAME = this.mediaName + '.' + FILE_EXTENSION
} catch (error) {
this.errorManager({
operation: 'set',
message: `Could not download post: "${ error.message }"`,
})
}
this.downloadBlobToDevice(RESPONSE_BLOB, FILE_NAME);
},
},
}
Expand Down

0 comments on commit 52d3496

Please sign in to comment.