使用可恢复方法上传到vimeo

时间:2019-02-16 11:10:55

标签: vue.js vimeo vimeo-api

我正在尝试在上传时获取视频总大小的上传字节,以便能够实现上传栏并在停止位置中断的情况下恢复上传,我正在将vimeo上传与可恢复的方法;上传效果很好,但是我受onUploadProgress函数的困扰。

            let vimeoAccessTokens = process.env.MIX_VIEMO_ACCESS_TOKEN
            let videoCreate = Axios.create()

            delete videoCreate.defaults.headers.common['X-CSRF-TOKEN']
            delete videoCreate.defaults.headers.common['X-Requested-With']

            videoCreate({
                method: 'POST',
                url: 'https://api.vimeo.com/me/videos',
                headers: {
                    'Authorization': 'bearer ' + vimeoAccessTokens,
                    'Accept': 'application/vnd.vimeo.*+json;version=3.4',
                    'Content-Type': 'application/json'
                },
                data: {
                    "upload": {
                        "approach": "tus",
                        "size": this.videoFile.size
                    }
                }
            }).then(response => {
                let videoUri = response.data.uri
                let uploadLink = response.data.upload.upload_link
                let videoUpload = Axios.create()

                videoUpload({
                    method: 'PATCH',
                    url: uploadLink,
                    headers: {
                        'Tus-Resumable': '1.0.0',
                        'Upload-Offset': 0,
                        'Content-Type': 'application/offset+octet-stream',
                        'Accept': 'application/vnd.vimeo.*+json;version=3.4'
                    },
                    data: this.videoFile,
                    onUploadProgress(){
                        Axios.head(uploadLink, {
                            headers: {
                                'Tus-Resumable': '1.0.0',
                                'Accept': 'application/vnd.vimeo.*+json;version=3.4'
                            }
                        }).then(uploadResp => {
                            let uploaded = uploadResp.headers['upload-offset']
                            let total = uploadResp.headers['upload-length']
                            console.info(uploaded +'of'+ total)
                        })
                    }
                }).then(resp => {
                    console.log(resp.status)
                }).catch(err => {
                    window.handleErrors(err)
                })
            }).catch(error => {
                window.handleErrors(error)
            })

            event.preventDefault()
        }

2 个答案:

答案 0 :(得分:2)

我可能迟到了,但让我把想法留在这里。我最近遇到了这个问题,但是用uppy-tus解决了。它带有一个不错的用户界面,对我来说非常酷,使您的工作变得超级轻松 这是使用Vue的示例代码

<script>
import Axios from 'axios'

const Uppy = require('@uppy/core')
const XHRUpload = require('@uppy/xhr-upload')
const Dashboard = require('@uppy/dashboard')
const tus = require("@uppy/tus")
require('@uppy/core/dist/style.css')
require('@uppy/dashboard/dist/style.css')
export default {
    data() {
        return {
            //store Vimeo access token in .env
            token: process.env.VUE_APP_VIMEO_ACCESS_TOKEN,
            url: '',
            video: '',
            video_file: {
                name: '',
                size: '',
                upload_link: ''
            },

            uppy: null

        }
    },
    methods: {
        //load uppy on mounted
        loadUppy() {
            this.uppy = Uppy({
                restrictions: {
                    maxNumberOfFiles: 1,
                    minNumberOfFiles: 1,
                    allowedFileTypes: ['video/*']
                },
                //
                onBeforeFileAdded: (currentFile, files) => {
                if (currentFile.name) {
                    console.log(currentFile)
                    this.video_file.name =currentFile.name
                    this.video_file.size = currentFile.data.size
                    this.video_file.type = currentFile.type
                    //console.log(this.video_file)
                    return true
                }
                return false
                },
            })
            .use(Dashboard, {
                trigger: '.UppyModalOpenerBtn',
                inline: true,
                target: '.DashboardContainer',
                replaceTargetContent: true,
                showProgressDetails: true,
                hideUploadButton: true,
                height: 250,
                allowMultipleUploads: false

            })



        },


        uploadVideo() {

            const body = {
            "upload" : {
                "approach" : "tus",
                "size" : this.video_file.size
            },
            "name" : this.video_file.name
            };
            // get vimeo upload link
            Axios.post('https://api.vimeo.com/me/videos/',body,{
                headers: {
                    'Content-Type': 'application/json',
                    'Accept': 'application/vnd.vimeo.*+json;version=3.4',
                    'Authorization': 'bearer ' + this.token
                }
            }).then((response) => {
                console.log(response)
                let videoUri = response.data.uri
                this.video_file.upload_link = response.data.upload.upload_link
                this.uppy.use(tus, { 
                    uploadUrl: this.video_file.upload_link,
                    metadata: {
                        filename: this.video_file.name,
                        filetype: this.video_file.type,
                    },
                    resume: true,
                    autoRetry: true,
                    retryDelays: [0, 1000, 3000, 5000]
                })
                this.uppy.upload().then((result) => {
                console.info('Successful uploads:', result.successful)

                if (result.failed.length > 0) {
                    console.error('Errors:')
                    result.failed.forEach((file) => {
                    console.error(file.error)
                    })
                }
                })



            }).catch((errors) => {
                console.log(errors)
            })
        },


    },

    mounted() {
        this.loadUppy()

    },

}

答案 1 :(得分:0)

Vimeo在使用可恢复方法上传时使用Tus协议,并且已经有一个纯的JavaScript客户端可用于tus可恢复上传协议。因此无需重新发明轮子。

此处: https://github.com/tus/tus-js-client