tweb-i2p/src/helpers/fileName.ts

67 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-04-08 13:52:31 +00:00
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
2021-05-03 20:02:53 +00:00
import type { InputFileLocation, InputStickerSet } from "../layer";
import type { DownloadOptions } from "../lib/mtproto/apiFileManager";
2020-10-29 17:11:09 +00:00
const FILENAME_JOINER = '_';
2021-05-03 20:02:53 +00:00
export function getFileNameByLocation(location: InputFileLocation, options?: Partial<{
2020-10-29 17:11:09 +00:00
fileName: string
}>) {
const fileName = '';//(options?.fileName || '').split('.');
const ext = fileName[fileName.length - 1] || '';
let str: string;
2020-10-29 17:11:09 +00:00
switch(location._) {
case 'inputPhotoFileLocation': {
str = ['photo', fileName[0], location.id, location.thumb_size].filter(Boolean).join(FILENAME_JOINER);
break;
}
2020-10-29 17:11:09 +00:00
case 'inputDocumentFileLocation': {
str = ['document', fileName[0], location.id, location.thumb_size].filter(Boolean).join(FILENAME_JOINER);
break;
2020-10-29 17:11:09 +00:00
}
case 'inputPeerPhotoFileLocation':
str = ['peerPhoto', location.photo_id, location.pFlags.big ? 'big' : 'small'].join(FILENAME_JOINER);
break;
2021-05-03 20:02:53 +00:00
case 'inputStickerSetThumb': {
const id = (location.stickerset as InputStickerSet.inputStickerSetID).id ||
(location.stickerset as InputStickerSet.inputStickerSetShortName).short_name ||
(location.stickerset as InputStickerSet.inputStickerSetDice).emoticon ||
location.stickerset._;
str = ['stickerSetThumb', id, location.thumb_version].join(FILENAME_JOINER);
break;
2021-05-03 20:02:53 +00:00
}
2020-10-29 17:11:09 +00:00
case 'inputFileLocation': {
str = location.volume_id + '_' + location.local_id;
break;
2020-10-29 17:11:09 +00:00
}
default: {
console.error('Unrecognized location:', location);
str = '';
break;
2020-10-29 17:11:09 +00:00
}
}
return str + (ext ? '.' + ext : ext);
}
export type FileURLType = 'photo' | 'thumb' | 'document' | 'stream' | 'download';
export function getFileURL(type: FileURLType, options: DownloadOptions) {
//console.log('getFileURL', location);
//const perf = performance.now();
const encoded = encodeURIComponent(JSON.stringify(options));
//console.log('getFileURL encode:', performance.now() - perf, encoded);
return '/' + type + '/' + encoded;
2021-05-03 20:02:53 +00:00
}