Telegram Web K with changes to work inside I2P
https://web.telegram.i2p/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.6 KiB
55 lines
1.6 KiB
/* |
|
* https://github.com/morethanwords/tweb |
|
* Copyright (C) 2019-2021 Eduard Kuzmenko |
|
* https://github.com/morethanwords/tweb/blob/master/LICENSE |
|
*/ |
|
|
|
import deferredPromise from '../../helpers/cancellablePromise'; |
|
import makeError from '../../helpers/makeError'; |
|
import fileNameRFC from '../../helpers/string/fileNameRFC'; |
|
import {getServiceMessagePort} from '../mtproto/mtproto.worker'; |
|
import DownloadWriter from './downloadWriter'; |
|
import FileStorage from './fileStorage'; |
|
import StreamWriter from './streamWriter'; |
|
|
|
export default class DownloadStorage implements FileStorage { |
|
public getFile(fileName: string): Promise<any> { |
|
return Promise.reject(makeError('NO_ENTRY_FOUND')); |
|
} |
|
|
|
public prepareWriting({fileName, downloadId, size}: { |
|
fileName: string, |
|
downloadId: string, |
|
size: number |
|
}) { |
|
const headers = { |
|
'Content-Type': 'application/octet-stream; charset=utf-8', |
|
'Content-Disposition': 'attachment; filename*=UTF-8\'\'' + fileNameRFC(fileName), |
|
...(size ? {'Content-Length': size} : {}) |
|
}; |
|
|
|
const serviceMessagePort = getServiceMessagePort(); |
|
const promise = serviceMessagePort.invoke('download', { |
|
headers, |
|
id: downloadId |
|
}); |
|
|
|
const deferred = deferredPromise<void>(); |
|
deferred.cancel = () => { |
|
deferred.reject(makeError('DOWNLOAD_CANCELED')); |
|
}; |
|
|
|
deferred.catch(() => { |
|
getServiceMessagePort().invoke('downloadCancel', downloadId); |
|
}); |
|
|
|
promise.then(deferred.resolve, deferred.reject); |
|
|
|
return { |
|
deferred, |
|
getWriter: () => { |
|
return new DownloadWriter(serviceMessagePort, downloadId); |
|
} |
|
}; |
|
} |
|
}
|
|
|