Browse Source

Fix undefined filename

master
Eduard Kuzmenko 2 years ago
parent
commit
3d1093c265
  1. 24
      src/environment/mimeTypeMap.ts
  2. 8
      src/global.d.ts
  3. 12
      src/layer.d.ts
  4. 9
      src/lib/appManagers/appDocsManager.ts
  5. 3
      src/lib/appManagers/appStickersManager.ts
  6. 2
      src/lib/appManagers/utils/docs/getDocumentDownloadOptions.ts
  7. 3
      src/lib/appManagers/utils/photos/getPhotoDownloadOptions.ts
  8. 16
      src/lib/mtproto/apiFileManager.ts
  9. 9
      src/scripts/in/schema_additional_params.json

24
src/environment/mimeTypeMap.ts

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
export const EXTENSION_MIME_TYPE_MAP: {[ext in MTFileExtension]: MTMimeType} = {
pdf: 'application/pdf',
tgv: 'application/x-tgwallpattern',
tgs: 'application/x-tgsticker',
json: 'application/json',
wav: 'audio/wav',
mp3: 'audio/mpeg',
ogg: 'audio/ogg',
jpeg: 'image/jpeg',
jpg: 'image/jpeg',
png: 'image/png',
gif: 'image/gif',
webp: 'image/webp',
mp4: 'video/mp4',
webm: 'video/webm',
mov: 'video/quicktime',
svg: 'image/svg+xml'
};
export const MIME_TYPE_EXTENSION_MAP: {[mimeType in MTMimeType]?: MTFileExtension} = {};
for(const ext in EXTENSION_MIME_TYPE_MAP) {
MIME_TYPE_EXTENSION_MAP[EXTENSION_MIME_TYPE_MAP[ext as MTFileExtension]] = ext as MTFileExtension;
}

8
src/global.d.ts vendored

@ -23,6 +23,14 @@ declare global { @@ -23,6 +23,14 @@ declare global {
type Long = string | number;
type MTLong = string;
type MTMimeType = 'video/quicktime' | 'image/gif' | 'image/jpeg' | 'application/pdf' |
'video/mp4' | 'image/webp' | 'audio/mpeg' | 'audio/ogg' | 'application/octet-stream' |
'application/x-tgsticker' | 'video/webm' | 'image/svg+xml' | 'image/png' | 'application/json' |
'application/x-tgwallpattern' | 'audio/wav';
type MTFileExtension = 'mov' | 'gif' | 'pdf' | 'jpg' | 'jpeg' | 'wav' |
'tgv' | 'tgs' | 'svg' | 'mp4' | 'webm' | 'webp' | 'mp3' | 'ogg' | 'json' | 'png';
type ApiFileManagerError = 'DOWNLOAD_CANCELED' | 'UPLOAD_CANCELED' | 'FILE_TOO_BIG' | 'REFERENCE_IS_NOT_REFRESHED';
type StorageError = 'STORAGE_OFFLINE' | 'NO_ENTRY_FOUND' | 'IDB_CREATE_TIMEOUT';
type ReferenceError = 'NO_NEW_CONTEXT';

12
src/layer.d.ts vendored

@ -3307,7 +3307,6 @@ export namespace Document { @@ -3307,7 +3307,6 @@ export namespace Document {
access_hash: string | number,
file_reference: Uint8Array | number[],
date: number,
mime_type: string,
video_thumbs?: Array<VideoSize>,
dc_id: number,
attributes: Array<DocumentAttribute>,
@ -3326,7 +3325,8 @@ export namespace Document { @@ -3326,7 +3325,8 @@ export namespace Document {
}>,
animated?: boolean,
supportsStreaming?: boolean,
size?: number
size?: number,
mime_type?: MTMimeType
};
}
@ -5924,20 +5924,20 @@ export namespace WebDocument { @@ -5924,20 +5924,20 @@ export namespace WebDocument {
url: string,
access_hash: string | number,
size: number,
mime_type: string,
attributes: Array<DocumentAttribute>,
h?: number,
w?: number
w?: number,
mime_type?: MTMimeType
};
export type webDocumentNoProxy = {
_: 'webDocumentNoProxy',
url: string,
size: number,
mime_type: string,
attributes: Array<DocumentAttribute>,
h?: number,
w?: number
w?: number,
mime_type?: MTMimeType
};
}

9
src/lib/appManagers/appDocsManager.ts

@ -24,17 +24,12 @@ import getDocumentInputFileLocation from './utils/docs/getDocumentInputFileLocat @@ -24,17 +24,12 @@ import getDocumentInputFileLocation from './utils/docs/getDocumentInputFileLocat
import getDocumentURL from './utils/docs/getDocumentURL';
import type {ThumbCache} from '../storages/thumbs';
import makeError from '../../helpers/makeError';
import {EXTENSION_MIME_TYPE_MAP} from '../../environment/mimeTypeMap';
export type MyDocument = Document.document;
// TODO: если залить картинку файлом, а потом перезайти в диалог - превьюшка заново скачается
const EXTENSION_MIME_TYPE_MAP = {
mov: 'video/quicktime',
gif: 'image/gif',
pdf: 'application/pdf'
};
type WallPaperId = WallPaper.wallPaper['id'];
let uploadWallPaperTempId = 0;
@ -340,7 +335,7 @@ export class AppDocsManager extends AppManager { @@ -340,7 +335,7 @@ export class AppDocsManager extends AppManager {
dc_id: 0,
file_reference: [],
id,
mime_type: file.type,
mime_type: file.type as MTMimeType,
size: file.size,
date: Date.now() / 1000,
pFlags: {},

3
src/lib/appManagers/appStickersManager.ts

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
*/
import type {MyDocument} from './appDocsManager';
import type {DownloadOptions} from '../mtproto/apiFileManager';
import {Document, InputFileLocation, InputStickerSet, MessagesAllStickers, MessagesFavedStickers, MessagesFeaturedStickers, MessagesFoundStickerSets, MessagesRecentStickers, MessagesStickers, MessagesStickerSet, PhotoSize, StickerPack, StickerSet, StickerSetCovered, Update} from '../../layer';
import {Modify} from '../../types';
import AppStorage from '../storage';
@ -384,7 +385,7 @@ export class AppStickersManager extends AppManager { @@ -384,7 +385,7 @@ export class AppStickersManager extends AppManager {
this.storage.set({[id]: stickerSet}, !needSave);
}
public getStickerSetThumbDownloadOptions(stickerSet: StickerSet.stickerSet) {
public getStickerSetThumbDownloadOptions(stickerSet: StickerSet.stickerSet): DownloadOptions {
const thumb = stickerSet.thumbs.find((thumb) => thumb._ === 'photoSize') as PhotoSize.photoSize;
const dcId = stickerSet.thumb_dc_id;

2
src/lib/appManagers/utils/docs/getDocumentDownloadOptions.ts

@ -11,7 +11,7 @@ import getDocumentInputFileLocation from './getDocumentInputFileLocation'; @@ -11,7 +11,7 @@ import getDocumentInputFileLocation from './getDocumentInputFileLocation';
export default function getDocumentDownloadOptions(doc: Document.document, thumb?: PhotoSize.photoSize | VideoSize, queueId?: number, onlyCache?: boolean): DownloadOptions {
const inputFileLocation = getDocumentInputFileLocation(doc, thumb?.type);
let mimeType: string;
let mimeType: MTMimeType;
if(thumb?._ === 'photoSize') {
mimeType = doc.sticker ? 'image/webp' : 'image/jpeg'/* doc.mime_type */;
} else {

3
src/lib/appManagers/utils/photos/getPhotoDownloadOptions.ts

@ -18,7 +18,7 @@ export default function getPhotoDownloadOptions(photo: MyPhoto | MyDocument, pho @@ -18,7 +18,7 @@ export default function getPhotoDownloadOptions(photo: MyPhoto | MyDocument, pho
}
// maybe it's a thumb
const isPhoto = (photoSize._ === 'photoSize' || photoSize._ === 'photoSizeProgressive') && photo.access_hash && photo.file_reference;
const isPhoto = !!((photoSize._ === 'photoSize' || photoSize._ === 'photoSizeProgressive') && photo.access_hash && photo.file_reference);
const location: InputFileLocation.inputPhotoFileLocation | InputFileLocation.inputDocumentFileLocation = {
_: isDocument ? 'inputDocumentFileLocation' : 'inputPhotoFileLocation',
id: photo.id,
@ -31,6 +31,7 @@ export default function getPhotoDownloadOptions(photo: MyPhoto | MyDocument, pho @@ -31,6 +31,7 @@ export default function getPhotoDownloadOptions(photo: MyPhoto | MyDocument, pho
dcId: photo.dc_id,
location,
size: isPhoto ? (photoSize as PhotoSize.photoSize).size : undefined,
mimeType: 'image/jpeg',
queueId,
onlyCache
};

16
src/lib/mtproto/apiFileManager.ts

@ -40,6 +40,7 @@ import readBlobAsUint8Array from '../../helpers/blob/readBlobAsUint8Array'; @@ -40,6 +40,7 @@ import readBlobAsUint8Array from '../../helpers/blob/readBlobAsUint8Array';
import DownloadStorage from '../files/downloadStorage';
import copy from '../../helpers/object/copy';
import indexOfAndSplice from '../../helpers/array/indexOfAndSplice';
import {MIME_TYPE_EXTENSION_MAP} from '../../environment/mimeTypeMap';
type Delayed = {
offset: number,
@ -52,7 +53,7 @@ export type DownloadOptions = { @@ -52,7 +53,7 @@ export type DownloadOptions = {
location: InputFileLocation | InputWebFileLocation,
size?: number,
fileName?: string,
mimeType?: string,
mimeType?: MTMimeType,
limitPart?: number,
queueId?: number,
onlyCache?: boolean,
@ -428,7 +429,7 @@ export class ApiFileManager extends AppManager { @@ -428,7 +429,7 @@ export class ApiFileManager extends AppManager {
return this.uploadPromises[fileName];
}
private getConvertMethod(mimeType: string) {
private getConvertMethod(mimeType: MTMimeType) {
let process: ApiFileManager['uncompressTGS'] | ApiFileManager['convertWebp'];
if(mimeType === 'application/x-tgwallpattern') {
process = this.uncompressTGV;
@ -558,8 +559,17 @@ export class ApiFileManager extends AppManager { @@ -558,8 +559,17 @@ export class ApiFileManager extends AppManager {
}
if(downloadStorage) {
let downloadFileName = options.fileName; // it's doc file_name
if(!downloadFileName) {
downloadFileName = cacheFileName;
const ext = MIME_TYPE_EXTENSION_MAP[options.mimeType];
if(ext) {
downloadFileName += '.' + ext;
}
}
downloadPrepared = downloadStorage.prepareWriting({
fileName: options.fileName, // it's doc file_name
fileName: downloadFileName,
downloadId,
size: possibleSize
});

9
src/scripts/in/schema_additional_params.json

@ -14,7 +14,8 @@ @@ -14,7 +14,8 @@
{"name": "stickerThumbConverted", "type": "true"},
{"name": "animated", "type": "boolean"},
{"name": "supportsStreaming", "type": "boolean"},
{"name": "size", "type": "number"}
{"name": "size", "type": "number"},
{"name": "mime_type", "type": "MTMimeType"}
]
}, {
"predicate": "photoSizeProgressive",
@ -349,12 +350,14 @@ @@ -349,12 +350,14 @@
"predicate": "webDocument",
"params": [
{"name": "h", "type": "number"},
{"name": "w", "type": "number"}
{"name": "w", "type": "number"},
{"name": "mime_type", "type": "MTMimeType"}
]
}, {
"predicate": "webDocumentNoProxy",
"params": [
{"name": "h", "type": "number"},
{"name": "w", "type": "number"}
{"name": "w", "type": "number"},
{"name": "mime_type", "type": "MTMimeType"}
]
}]
Loading…
Cancel
Save