Browse Source

Fix setting background patterns

master
Eduard Kuzmenko 3 years ago
parent
commit
abeebe0317
  1. 4
      src/components/chat/bubbles.ts
  2. 6
      src/helpers/blob/blobConstruct.ts
  3. 6
      src/lib/cacheStorage.ts
  4. 30
      src/lib/fileManager.ts
  5. 2
      src/lib/idb.ts
  6. 28
      src/lib/mtproto/apiFileManager.ts
  7. 2
      src/lib/rlottie/lottieLoader.ts

4
src/components/chat/bubbles.ts

@ -847,7 +847,7 @@ export default class ChatBubbles {
this.onUnreadedInViewport(target, mid); this.onUnreadedInViewport(target, mid);
} }
}); });
}); }, {root: this.scrollable.container});
this.viewsObserver = new IntersectionObserver((entries) => { this.viewsObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => { entries.forEach(entry => {
@ -870,7 +870,7 @@ export default class ChatBubbles {
} }
} }
}); });
}); }, {root: this.scrollable.container});
this.sendViewCountersDebounced = debounce(() => { this.sendViewCountersDebounced = debounce(() => {
const mids = [...this.viewsMids]; const mids = [...this.viewsMids];

6
src/helpers/blob/blobConstruct.ts

@ -11,7 +11,11 @@
import blobSafeMimeType from "./blobSafeMimeType"; import blobSafeMimeType from "./blobSafeMimeType";
export default function blobConstruct(blobParts: any, mimeType: string = ''): Blob { export default function blobConstruct<T extends Uint8Array | string>(blobParts: Array<T> | T, mimeType: string = ''): Blob {
if(!Array.isArray(blobParts)) {
blobParts = [blobParts];
}
let blob; let blob;
const safeMimeType = blobSafeMimeType(mimeType); const safeMimeType = blobSafeMimeType(mimeType);
try { try {

6
src/lib/cacheStorage.ts

@ -78,7 +78,7 @@ export default class CacheStorageController {
public saveFile(fileName: string, blob: Blob | Uint8Array) { public saveFile(fileName: string, blob: Blob | Uint8Array) {
//return Promise.resolve(blobConstruct([blob])); //return Promise.resolve(blobConstruct([blob]));
if(!(blob instanceof Blob)) { if(!(blob instanceof Blob)) {
blob = blobConstruct(blob) as Blob; blob = blobConstruct(blob);
} }
const response = new Response(blob, { const response = new Response(blob, {
@ -123,8 +123,8 @@ export default class CacheStorageController {
}); });
} }
public getFileWriter(fileName: string, mimeType: string) { public getFileWriter(fileName: string, fileSize: number, mimeType: string) {
const fakeWriter = FileManager.getFakeFileWriter(mimeType, (blob) => { const fakeWriter = FileManager.getFakeFileWriter(mimeType, fileSize, (blob) => {
return this.saveFile(fileName, blob).catch(() => blob); return this.saveFile(fileName, blob).catch(() => blob);
}); });

30
src/lib/fileManager.ts

@ -10,7 +10,6 @@
*/ */
import blobConstruct from "../helpers/blob/blobConstruct"; import blobConstruct from "../helpers/blob/blobConstruct";
import readBlobAsUint8Array from "../helpers/blob/readBlobAsUint8Array";
export class FileManager { export class FileManager {
private blobSupported = true; private blobSupported = true;
@ -27,37 +26,34 @@ export class FileManager {
return this.blobSupported; return this.blobSupported;
} }
public write(fileWriter: ReturnType<FileManager['getFakeFileWriter']>, bytes: Uint8Array | Blob | string): Promise<void> { public getFakeFileWriter(mimeType: string, size: number, saveFileCallback?: (blob: Blob) => Promise<Blob>) {
if(bytes instanceof Blob) { // is file bytes let bytes: Uint8Array = new Uint8Array(size);
return readBlobAsUint8Array(bytes).then(arr => {
return fileWriter.write(arr);
});
} else {
return fileWriter.write(bytes);
}
}
public getFakeFileWriter(mimeType: string, saveFileCallback?: (blob: Blob) => Promise<Blob>) {
const blobParts: Array<Uint8Array | string> = [];
const fakeFileWriter = { const fakeFileWriter = {
write: async(part: Uint8Array | string) => { write: async(part: Uint8Array, offset: number) => {
if(!this.blobSupported) { if(!this.blobSupported) {
throw false; throw false;
} }
blobParts.push(part); bytes.set(part, offset);
}, },
truncate: () => { truncate: () => {
blobParts.length = 0; bytes = new Uint8Array();
},
trim: (size: number) => {
bytes = bytes.slice(0, size);
}, },
finalize: (saveToStorage = true) => { finalize: (saveToStorage = true) => {
const blob = blobConstruct(blobParts, mimeType); const blob = blobConstruct(bytes, mimeType);
if(saveToStorage && saveFileCallback) { if(saveToStorage && saveFileCallback) {
saveFileCallback(blob); saveFileCallback(blob);
} }
return blob; return blob;
},
getParts: () => bytes,
replaceParts: (parts: typeof bytes) => {
bytes = parts;
} }
}; };

2
src/lib/idb.ts

@ -260,7 +260,7 @@ export default class IDBStorage<T extends Database<any>> {
public saveFile(fileName: string, blob: Blob | Uint8Array) { public saveFile(fileName: string, blob: Blob | Uint8Array) {
//return Promise.resolve(blobConstruct([blob])); //return Promise.resolve(blobConstruct([blob]));
if(!(blob instanceof Blob)) { if(!(blob instanceof Blob)) {
blob = blobConstruct([blob]) as Blob; blob = blobConstruct(blob);
} }
return this.save(fileName, blob); return this.save(fileName, blob);

28
src/lib/mtproto/apiFileManager.ts

@ -272,13 +272,13 @@ export class ApiFileManager {
private uncompressTGS = (bytes: Uint8Array, fileName: string) => { private uncompressTGS = (bytes: Uint8Array, fileName: string) => {
//this.log('uncompressTGS', bytes, bytes.slice().buffer); //this.log('uncompressTGS', bytes, bytes.slice().buffer);
// slice нужен потому что в uint8array - 5053 length, в arraybuffer - 5084 // slice нужен потому что в uint8array - 5053 length, в arraybuffer - 5084
return cryptoWorker.invokeCrypto('gzipUncompress', bytes.slice().buffer, true) as Promise<string>; return cryptoWorker.invokeCrypto('gzipUncompress', bytes.slice().buffer, false) as Promise<Uint8Array>;
}; };
private uncompressTGV = (bytes: Uint8Array, fileName: string) => { private uncompressTGV = (bytes: Uint8Array, fileName: string) => {
//this.log('uncompressTGS', bytes, bytes.slice().buffer); //this.log('uncompressTGS', bytes, bytes.slice().buffer);
// slice нужен потому что в uint8array - 5053 length, в arraybuffer - 5084 // slice нужен потому что в uint8array - 5053 length, в arraybuffer - 5084
return cryptoWorker.invokeCrypto('gzipUncompress', bytes.slice().buffer, true) as Promise<string>; return cryptoWorker.invokeCrypto('gzipUncompress', bytes.slice().buffer, false) as Promise<Uint8Array>;
}; };
private convertWebp = (bytes: Uint8Array, fileName: string) => { private convertWebp = (bytes: Uint8Array, fileName: string) => {
@ -408,11 +408,11 @@ export class ApiFileManager {
deferred.resolve(blob); deferred.resolve(blob);
}).catch(() => { }).catch(() => {
//this.log('not cached', fileName); //this.log('not cached', fileName);
const fileWriterPromise = fileStorage.getFileWriter(fileName, mimeType); const limit = options.limitPart || this.getLimitPart(size);
const fileWriterPromise = fileStorage.getFileWriter(fileName, size || limit, mimeType);
fileWriterPromise.then((fileWriter) => { fileWriterPromise.then((fileWriter) => {
cacheFileWriter = fileWriter; cacheFileWriter = fileWriter;
const limit = options.limitPart || this.getLimitPart(size);
let offset: number; let offset: number;
let startOffset = 0; let startOffset = 0;
let writeFilePromise: CancellablePromise<void> = Promise.resolve(), let writeFilePromise: CancellablePromise<void> = Promise.resolve(),
@ -422,7 +422,7 @@ export class ApiFileManager {
//console.error('maxRequests', maxRequests); //console.error('maxRequests', maxRequests);
const processDownloaded = async(bytes: Uint8Array, offset: number) => { const processDownloaded = async(bytes: Uint8Array) => {
if(process) { if(process) {
//const perf = performance.now(); //const perf = performance.now();
const processed = await process(bytes, fileName); const processed = await process(bytes, fileName);
@ -473,13 +473,18 @@ export class ApiFileManager {
deferred.notify({done, offset, total: size}); deferred.notify({done, offset, total: size});
//} //}
const processedResult = await processDownloaded(bytes, offset); await writeFilePromise;
checkCancel(); checkCancel();
await writeFilePromise; await fileWriter.write(bytes, offset);
}
if(isFinal && process) {
const bytes = fileWriter.getParts();
const processedResult = await processDownloaded(bytes);
checkCancel(); checkCancel();
await fileManager.write(fileWriter, processedResult); fileWriter.replaceParts(processedResult);
} }
writeFileDeferred.resolve(); writeFileDeferred.resolve();
@ -487,7 +492,12 @@ export class ApiFileManager {
if(isFinal) { if(isFinal) {
resolved = true; resolved = true;
deferred.resolve(fileWriter.finalize(size < MAX_FILE_SAVE_SIZE)); const realSize = size || bytes.byteLength;
if(!size) {
fileWriter.trim(realSize);
}
deferred.resolve(fileWriter.finalize(realSize < MAX_FILE_SAVE_SIZE));
} }
} catch(err) { } catch(err) {
errorHandler(err as Error); errorHandler(err as Error);

2
src/lib/rlottie/lottieLoader.ts

@ -101,7 +101,7 @@ export class LottieLoader {
return fetch(url) return fetch(url)
.then(res => { .then(res => {
if(!res.headers || res.headers.get('content-type') === 'application/octet-stream') { if(!res.headers || res.headers.get('content-type') === 'application/octet-stream') {
return res.arrayBuffer().then(data => apiManager.invokeCrypto('gzipUncompress', data)).then(arr => blobConstruct([arr], '')) return res.arrayBuffer().then(data => apiManager.invokeCrypto('gzipUncompress', data)).then(arr => blobConstruct(arr as Uint8Array, ''))
} else { } else {
return res.blob(); return res.blob();
} }

Loading…
Cancel
Save