Skip to content

Commit

Permalink
Add ability to process downloads
Browse files Browse the repository at this point in the history
- Download manager now automaticallly begins processing when it receives work, but it won't work through a long queue yet, it always stops after 1

#348
  • Loading branch information
MarmadileManteater committed Jul 16, 2024
1 parent a28d65b commit c01abe6
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default defineComponent({
},
data: function () {
return {
fileName: '',
selected: -1,
captionSelected: -1,
audioTrackSelected: -1
Expand Down Expand Up @@ -177,7 +178,8 @@ export default defineComponent({
format: this.audioVideoSources[this.formatSelected],
captions: this.captions[this.captionSelected],
// -1 means default to what is paired in formats
languageTrackSelected: this.audioTrackSelected
languageTrackSelected: this.audioTrackSelected,
fileName: `${(this.fileName !== '' ? this.fileName : this.placeholderTitle)}${this.container}`
}
await addToDownloadQueue(downloadRequest)
showToast(this.$t('Download Prompt.Video has been added to download queue'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
:label="'File name'"
:show-label="true"
:placeholder="placeholderTitle"
:value="fileName"
:show-action-button="false"
/>
<div class="type">{{ container }}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { mapActions } from 'vuex'
import FtFlexBox from '../ft-flex-box/ft-flex-box.vue'
import FtPrompt from '../ft-prompt/ft-prompt.vue'
import FtButton from '../ft-button/ft-button.vue'
import { getQueueDirectory, readFile } from '../../helpers/android'
import { downloadVideoAndAudio, getDownloadsDirectory, getNestedUri, getQueueDirectory, getVideosDirectory, readFile, writeFile } from '../../helpers/android'
import { getRelativeTimeFromDate } from '../../helpers/utils'

import android from 'android'
export default defineComponent({
name: 'FtaDownloadManager',
components: {
Expand All @@ -15,7 +15,8 @@ export default defineComponent({
},
data() {
return {
queue: []
queue: [],
isWorking: false
}
},
computed: {
Expand All @@ -33,7 +34,7 @@ export default defineComponent({
return result
}
},
async mounted() {
async created() {
await this.scanQueueDirectory()
window.addEventListener('add-to-download-queue', this.addToDownloadQueue)
},
Expand All @@ -42,6 +43,62 @@ export default defineComponent({
window.removeEventListener('add-to-download-queue', this.addToDownloadQueue)
},
methods: {
async work() {
if (this.queue.length > 0 && !this.isWorking) {
this.isWorking = true
// something to work on
const item = this.queue[0]
/** @type {import('../../helpers/android').AudioVideo} */
const format = item.format
const videosDirectory = await getVideosDirectory()
try {
await downloadVideoAndAudio(videosDirectory, format.video, format.audio, item.videoData.id, (message) => {
console.log(message)
})
const videoOutputDirectory = await getNestedUri(videosDirectory, item.videoData.id)
const outputFiles = await videoOutputDirectory.listFiles()
let output = null
let video = null
let audio = null
for (const file in outputFiles) {
if (outputFiles[file].fileName.startsWith('output')) {
output = outputFiles[file]
}
if (outputFiles[file].fileName.startsWith('video')) {
video = outputFiles[file]
}
if (outputFiles[file].fileName.startsWith('audio')) {
audio = outputFiles[file]
}
}
if (output !== null) {
android.renameFile(output.uri, item.fileName)
}
// clean-up after ffmpeg
if (video !== null) {
android.deleteFileInTree(video.uri)
}
if (audio !== null) {
android.deleteFileInTree(audio.uri)
}
const finishedOutput = await videoOutputDirectory.listFiles()
if (finishedOutput.length > 0) {
const data = item.videoData
data.uri = finishedOutput[0].uri
const jsonUri = videoOutputDirectory.createFile('data.json')
await writeFile(jsonUri, JSON.stringify(data, null, 2))
}
this.queue = this.queue.slice(1)
} catch (ex) {
console.error(ex)
}
this.isWorking = false
if (this.queue.length > 0) {
// todo put optional delay + recall work
console.log('more work to do')
}
}
},
getRelativeTimeFromDate() {
return getRelativeTimeFromDate(...arguments)
},
Expand All @@ -59,6 +116,9 @@ export default defineComponent({
},
async addToDownloadQueue({ data }) {
this.queue.push(data)
if (!this.isWorking) {
this.work()
}
},
...mapActions([
'showDownloadManager',
Expand Down
15 changes: 12 additions & 3 deletions src/renderer/helpers/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,26 @@ export async function getQueueDirectory() {
}
}

export async function getVideosDirectory() {
const downloads = await getDownloadsDirectory()
const files = await downloads.listFiles()
const results = files.filter(file => file.fileName === 'videos')
if (results.length > 0) {
return results[0]
}
}

export async function getNestedUri(handle, path) {
/** @type {Array<string>} */
let parts = path.split('/')
let parts = path.indexOf('/') !== -1 ? path.split('/') : [path]
/** @type {DirectoryHandle} */
let runningHandle = handle
while (parts.length > 0) {
const startingLength = parts.length
const ls = await runningHandle.listFiles()
for (const file in ls) {
if (file === parts[0]) {
runningHandle = file
if (ls[file].fileName === parts[0]) {
runningHandle = ls[file]
parts = parts.slice(1)
}
}
Expand Down

0 comments on commit c01abe6

Please sign in to comment.