2020-10-31 12:47:42 +01:00
|
|
|
function readFile(file, arrayBuffer, encoding){
|
|
|
|
var reader = new FileReader()
|
|
|
|
var promise = pageEvents.load(reader).then(event => event.target.result)
|
|
|
|
reader[arrayBuffer ? "readAsArrayBuffer" : "readAsText"](file, encoding)
|
|
|
|
return promise
|
|
|
|
}
|
2020-10-29 06:07:56 +01:00
|
|
|
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){
|
2020-10-31 12:47:42 +01:00
|
|
|
return this.blob().then(blob => readFile(blob, false, encoding))
|
2020-10-29 06:07:56 +01:00
|
|
|
}else{
|
|
|
|
return loader.ajax(this.url)
|
|
|
|
}
|
|
|
|
}
|
2020-10-31 12:47:42 +01:00
|
|
|
blob(){
|
|
|
|
return this.arrayBuffer().then(response => new Blob([response]))
|
|
|
|
}
|
2020-10-29 06:07:56 +01:00
|
|
|
}
|
|
|
|
class LocalFile{
|
|
|
|
constructor(file){
|
|
|
|
this.file = file
|
|
|
|
this.path = file.webkitRelativePath
|
|
|
|
this.url = this.path
|
|
|
|
this.name = file.name
|
|
|
|
}
|
|
|
|
arrayBuffer(){
|
2020-10-31 12:47:42 +01:00
|
|
|
return readFile(this.file, true)
|
2020-10-29 06:07:56 +01:00
|
|
|
}
|
|
|
|
read(encoding){
|
2020-10-31 12:47:42 +01:00
|
|
|
return readFile(this.file, false, encoding)
|
|
|
|
}
|
|
|
|
blob(){
|
|
|
|
return Promise.resolve(this.file)
|
2020-10-29 06:07:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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){
|
2020-10-31 12:47:42 +01:00
|
|
|
return this.blob().then(blob => readFile(blob, false, encoding))
|
2020-10-29 06:07:56 +01:00
|
|
|
}else{
|
|
|
|
return gpicker.downloadFile(this.id)
|
|
|
|
}
|
|
|
|
}
|
2020-10-31 12:47:42 +01:00
|
|
|
blob(){
|
|
|
|
return this.arrayBuffer().then(response => new Blob([response]))
|
|
|
|
}
|
2020-10-29 06:07:56 +01:00
|
|
|
}
|
|
|
|
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()
|
|
|
|
}
|
2020-10-31 12:47:42 +01:00
|
|
|
blob(){
|
|
|
|
return this.arrayBuffer().then(response => new Blob([response]))
|
|
|
|
}
|
2020-10-29 06:07:56 +01:00
|
|
|
}
|