From 74e695a3204281d655ac7d0be4495bbbab1c3bdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=93=E3=81=91=E3=81=A3=E3=81=A1?= Date: Sat, 12 Mar 2022 02:11:15 +0900 Subject: [PATCH] fix: wipppppppppppppppppppppp --- .../client/src/scripts/select-file-enc.ts | 82 ++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/packages/client/src/scripts/select-file-enc.ts b/packages/client/src/scripts/select-file-enc.ts index afa24fffe..7f6b1a913 100644 --- a/packages/client/src/scripts/select-file-enc.ts +++ b/packages/client/src/scripts/select-file-enc.ts @@ -4,7 +4,6 @@ import { stream } from '@/stream'; import { i18n } from '@/i18n'; import { defaultStore } from '@/store'; import { DriveFile } from 'misskey-js/built/entities'; -const convert = require( 'convert-video'); function select(src: any, label: string | null, multiple: boolean): Promise { return new Promise((res, rej) => { @@ -16,12 +15,91 @@ function select(src: any, label: string | null, multiple: boolean): Promise { + //ここから + // @ts-ignore + function VideoConverter (videoFileData, targetFormat) { + try { + targetFormat = targetFormat.toLowerCase(); + let reader = new FileReader(); + return new Promise(resolve => { + reader.onload = function (event) { + let contentType = 'video/'+targetFormat; + + // @ts-ignore + let data = event.target.result.split(','); + let b64Data = data[1]; + let blob = getBlobFromBase64Data(b64Data, contentType); + let blobUrl = URL.createObjectURL(blob); + + let convertedVideo = { + name: videoFileData.name.substring(0, videoFileData.name.lastIndexOf(".")), + format: targetFormat, + data: blobUrl + } + // console.log("convertedVideo: ", convertedVideo); + resolve(convertedVideo); + } + reader.readAsDataURL(videoFileData); + }); + + } catch (e) { + console.log("Error occurred while converting : ", e); + } + } + + function getBlobFromBase64Data(b64Data, contentType, sliceSize=512) { + const byteCharacters = atob(b64Data); + const byteArrays = []; + + for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) { + const slice = byteCharacters.slice(offset, offset + sliceSize); + + const byteNumbers = new Array(slice.length); + for (let i = 0; i < slice.length; i++) { + byteNumbers[i] = slice.charCodeAt(i); + } + + const byteArray = new Uint8Array(byteNumbers); + // @ts-ignore + byteArrays.push(byteArray); + } + + const blob = new Blob(byteArrays, {type: contentType}); + return blob; + } + + //ここまで + + /* + MIT License + + Copyright (c) 2021 Suvro Debnath + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + */ async function convertVideo(input) { let sourceVideoFile = input.files[0]; let targetVideoFormat = 'mp4' - let convertedVideoDataObj = await convert(sourceVideoFile, targetVideoFormat); + let convertedVideoDataObj = await VideoConverter(sourceVideoFile, targetVideoFormat); + // @ts-ignore let convertedVideoFile = new File([convertedVideoDataObj.data], convertedVideoDataObj.name + "." + convertedVideoDataObj.format); os.upload(convertedVideoFile, defaultStore.state.uploadFolder, undefined, keepOriginal.value).then(res).catch(e => { os.alert({ type: 'error', text: e }) });