Skip to content

Commit 4f0a90f

Browse files
authored
Merge pull request #299 from opcodesio/bug/downloading-files-with-headers
Bug / downloading files with headers
2 parents dc41ee8 + 3ccb590 commit 4f0a90f

File tree

10 files changed

+2134
-8985
lines changed

10 files changed

+2134
-8985
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"vue": "^3.2.47",
3434
"vue-loader": "^17.0.1",
3535
"vue-router": "^4.1.6",
36-
"vue-template-compiler": "^2.7.14"
36+
"vue-template-compiler": "^2.7.14",
37+
"streamsaver": "^2.0.6"
3738
}
3839
}

public/app.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/app.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/app.js.LICENSE.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* @license MIT
66
*/
77

8+
/*! #__NO_SIDE_EFFECTS__ */
9+
810
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
911

1012
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */

public/mix-manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"/app.js": "/app.js?id=a9c7dc2c5d650205333810ac101da5e4",
3-
"/app.css": "/app.css?id=1d3e1405b5f756d27f515e824e61f265",
2+
"/app.js": "/app.js?id=0c6cf8ac8d5abf3598949d9417bf56eb",
3+
"/app.css": "/app.css?id=df9446e9defec9b688bb5566dc40e285",
44
"/img/log-viewer-128.png": "/img/log-viewer-128.png?id=d576c6d2e16074d3f064e60fe4f35166",
55
"/img/log-viewer-32.png": "/img/log-viewer-32.png?id=f8ec67d10f996aa8baf00df3b61eea6d",
66
"/img/log-viewer-64.png": "/img/log-viewer-64.png?id=8902d596fc883ca9eb8105bb683568c6"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<script setup>
2+
import { CloudArrowDownIcon } from '@heroicons/vue/24/outline';
3+
import streamSaver from 'streamsaver';
4+
import axios from 'axios';
5+
6+
const props = defineProps(['url']);
7+
8+
const downloadFile = async () => {
9+
const response = await axios.get(props.url, {
10+
responseType: 'blob'
11+
});
12+
13+
if (response.status !== 200) {
14+
throw new Error(`HTTP error! status: ${response.status}`);
15+
}
16+
17+
const disposition = response.headers['content-disposition'];
18+
const filename = disposition ? disposition.split('filename=')[1].replace(/"/g, '') : 'download.txt';
19+
20+
const fileStream = streamSaver.createWriteStream(filename);
21+
const readableStream = response.data.stream();
22+
23+
if (window.WritableStream && readableStream.pipeTo) {
24+
return readableStream.pipeTo(fileStream)
25+
.then(() => console.log('done writing'));
26+
}
27+
28+
window.writer = fileStream.getWriter();
29+
const reader = readableStream.getReader();
30+
const pump = () => reader.read()
31+
.then(res => res.done
32+
? writer.close()
33+
: writer.write(res.value).then(pump));
34+
35+
pump();
36+
};</script>
37+
38+
<template>
39+
<button @click="downloadFile">
40+
<slot>
41+
<CloudArrowDownIcon class="w-4 h-4 mr-2" />
42+
Download
43+
</slot>
44+
</button>
45+
</template>

resources/js/components/FileList.vue

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,7 @@
135135
</MenuItem>
136136

137137
<MenuItem v-if="folder.can_download" v-slot="{ active }">
138-
<a :href="folder.download_url" download @click.stop :class="[active ? 'active' : '']">
139-
<CloudArrowDownIcon class="w-4 h-4 mr-2"/>
140-
Download
141-
</a>
138+
<DownloadLink :url="folder.download_url" @click.stop :class="[active ? 'active' : '']" />
142139
</MenuItem>
143140

144141
<template v-if="folder.can_delete">
@@ -201,7 +198,6 @@ import {
201198
ArrowLeftIcon,
202199
ArrowPathIcon,
203200
CircleStackIcon,
204-
CloudArrowDownIcon,
205201
EllipsisVerticalIcon,
206202
ExclamationTriangleIcon,
207203
FolderIcon,
@@ -220,6 +216,7 @@ import SiteSettingsDropdown from './SiteSettingsDropdown.vue';
220216
import HostSelector from './HostSelector.vue';
221217
import { handleKeyboardFileNavigation, handleKeyboardFileSettingsNavigation } from '../keyboardNavigation';
222218
import FileTypeSelector from './FileTypeSelector.vue';
219+
import DownloadLink from "./DownloadLink.vue";
223220
224221
const router = useRouter();
225222
const route = useRoute();

resources/js/components/FileListItem.vue

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@
4545
</MenuItem>
4646

4747
<MenuItem v-if="logFile.can_download" @click.stop v-slot="{ active }">
48-
<a :href="logFile.download_url" download :class="[active ? 'active' : '']">
49-
<CloudArrowDownIcon class="w-4 h-4 mr-2" />
50-
Download
51-
</a>
48+
<DownloadLink :url="logFile.download_url" :class="[active ? 'active' : '']" />
5249
</MenuItem>
5350

5451
<template v-if="logFile.can_delete">
@@ -78,12 +75,13 @@
7875
<script setup>
7976
import { computed } from 'vue';
8077
import { Menu, MenuButton, MenuItem, MenuItems } from '@headlessui/vue';
81-
import { CircleStackIcon, CloudArrowDownIcon, EllipsisVerticalIcon, TrashIcon } from '@heroicons/vue/24/outline';
78+
import { CircleStackIcon, EllipsisVerticalIcon, TrashIcon } from '@heroicons/vue/24/outline';
8279
import { useFileStore } from '../stores/files.js';
8380
import SpinnerIcon from './SpinnerIcon.vue';
8481
import { replaceQuery, useDropdownDirection } from '../helpers.js';
8582
import { useRouter } from 'vue-router';
8683
import { handleKeyboardFileNavigation, handleKeyboardFileSettingsNavigation } from '../keyboardNavigation';
84+
import DownloadLink from "./DownloadLink.vue";
8785
8886
const props = defineProps({
8987
logFile: {

resources/js/stores/search.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineStore } from 'pinia';
2+
import axios from 'axios';
23

34
export const useSearchStore = defineStore({
45
id: 'search',
@@ -40,9 +41,9 @@ export const useSearchStore = defineStore({
4041
const queryChecked = this.query;
4142
if (queryChecked === '') return;
4243
const queryParams = '?' + new URLSearchParams({ query: queryChecked });
43-
fetch(this.searchMoreRoute + queryParams)
44-
.then((response) => response.json())
45-
.then((data) => {
44+
axios.get(this.searchMoreRoute + queryParams)
45+
.then((response) => {
46+
const data = response.data;
4647
if (this.query !== queryChecked) return;
4748
const wasPreviouslySearching = this.searching;
4849
this.searching = data.hasMoreResults;

0 commit comments

Comments
 (0)