Files
taiko-web/public/src/js/abstractfile.js
LoveEevee 3fea149353 ImportSongs: Add Google Drive support
- Adds a new page for importing custom songs, where it is possible to pick a local folder (desktop only) or a Google Drive folder (desktop and Android)
  - This feature is disabled on iOS due to the lack of OGG audio support in the browser
- In order to not get rate limited, a TJA file is parsed for metadata only when the song is clicked in the song selection, rather than all at once at import time
- The instance maintainer will need to provide the API credentials in the config.py file to enable this feature
  - This requires a new project to be created at console.cloud.google.com
  - Drive API will have to be enabled
  - API and OAuth keys should be created
    - API key can be restricted to only have Google Drive and Google Picker APIs
    - OAuth Client ID should have Web Application type and JavaScript origins set
    - Editing the OAuth consent screen to have a name and icon is recommended
      - It is semi-required to submit the consent screen for verification as the permission to download all of the Drive files will be asked.
      - Note that the email of the maintainer is publicly visible on the consent screen
  - The project number can be found in the IAM & Admin settings page
2020-10-29 08:07:56 +03:00

88 lines
1.8 KiB
JavaScript

class RemoteFile{
constructor(url){
this.url = url
try{
this.path = new URL(url).pathname
}catch(e){
this.path = url
}
if(this.path.startsWith("/")){
this.path = this.path.slice(1)
}
this.name = this.path
var index = this.name.lastIndexOf("/")
if(index !== -1){
this.name = this.name.slice(index + 1)
}
}
arrayBuffer(){
return loader.ajax(this.url, request => {
request.responseType = "arraybuffer"
})
}
read(encoding){
if(encoding){
return this.arrayBuffer().then(response =>
new TextDecoder(encoding).decode(response)
)
}else{
return loader.ajax(this.url)
}
}
}
class LocalFile{
constructor(file){
this.file = file
this.path = file.webkitRelativePath
this.url = this.path
this.name = file.name
}
arrayBuffer(){
var reader = new FileReader()
var promise = pageEvents.load(reader).then(event => event.target.result)
reader.readAsArrayBuffer(this.file)
return promise
}
read(encoding){
var reader = new FileReader()
var promise = pageEvents.load(reader).then(event => event.target.result)
reader.readAsText(this.file, encoding)
return promise
}
}
class GdriveFile{
constructor(fileObj){
this.path = fileObj.path
this.name = fileObj.name
this.id = fileObj.id
this.url = gpicker.filesUrl + this.id + "?alt=media"
}
arrayBuffer(){
return gpicker.downloadFile(this.id, true)
}
read(encoding){
if(encoding){
return this.arrayBuffer().then(response =>
new TextDecoder(encoding).decode(response)
)
}else{
return gpicker.downloadFile(this.id)
}
}
}
class CachedFile{
constructor(contents, oldFile){
this.contents = contents
this.oldFile = oldFile
this.path = oldFile.path
this.name = oldFile.name
this.url = oldFile.url
}
arrayBuffer(){
return Promise.resolve(this.contents)
}
read(encoding){
return this.arrayBuffer()
}
}