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

6
src/helpers/blob/blobConstruct.ts

@ -11,7 +11,11 @@ @@ -11,7 +11,11 @@
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;
const safeMimeType = blobSafeMimeType(mimeType);
try {

6
src/lib/cacheStorage.ts

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

30
src/lib/fileManager.ts

@ -10,7 +10,6 @@ @@ -10,7 +10,6 @@
*/
import blobConstruct from "../helpers/blob/blobConstruct";
import readBlobAsUint8Array from "../helpers/blob/readBlobAsUint8Array";
export class FileManager {
private blobSupported = true;
@ -27,37 +26,34 @@ export class FileManager { @@ -27,37 +26,34 @@ export class FileManager {
return this.blobSupported;
}
public write(fileWriter: ReturnType<FileManager['getFakeFileWriter']>, bytes: Uint8Array | Blob | string): Promise<void> {
if(bytes instanceof Blob) { // is file bytes
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> = [];
public getFakeFileWriter(mimeType: string, size: number, saveFileCallback?: (blob: Blob) => Promise<Blob>) {
let bytes: Uint8Array = new Uint8Array(size);
const fakeFileWriter = {
write: async(part: Uint8Array | string) => {
write: async(part: Uint8Array, offset: number) => {
if(!this.blobSupported) {
throw false;
}
blobParts.push(part);
bytes.set(part, offset);
},
truncate: () => {
blobParts.length = 0;
bytes = new Uint8Array();
},
trim: (size: number) => {
bytes = bytes.slice(0, size);
},
finalize: (saveToStorage = true) => {
const blob = blobConstruct(blobParts, mimeType);
const blob = blobConstruct(bytes, mimeType);
if(saveToStorage && saveFileCallback) {
saveFileCallback(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>> { @@ -260,7 +260,7 @@ export default class IDBStorage<T extends Database<any>> {
public saveFile(fileName: string, blob: Blob | Uint8Array) {
//return Promise.resolve(blobConstruct([blob]));
if(!(blob instanceof Blob)) {
blob = blobConstruct([blob]) as Blob;
blob = blobConstruct(blob);
}
return this.save(fileName, blob);

28
src/lib/mtproto/apiFileManager.ts

@ -272,13 +272,13 @@ export class ApiFileManager { @@ -272,13 +272,13 @@ export class ApiFileManager {
private uncompressTGS = (bytes: Uint8Array, fileName: string) => {
//this.log('uncompressTGS', bytes, bytes.slice().buffer);
// 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) => {
//this.log('uncompressTGS', bytes, bytes.slice().buffer);
// 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) => {
@ -408,11 +408,11 @@ export class ApiFileManager { @@ -408,11 +408,11 @@ export class ApiFileManager {
deferred.resolve(blob);
}).catch(() => {
//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) => {
cacheFileWriter = fileWriter;
const limit = options.limitPart || this.getLimitPart(size);
let offset: number;
let startOffset = 0;
let writeFilePromise: CancellablePromise<void> = Promise.resolve(),
@ -422,7 +422,7 @@ export class ApiFileManager { @@ -422,7 +422,7 @@ export class ApiFileManager {
//console.error('maxRequests', maxRequests);
const processDownloaded = async(bytes: Uint8Array, offset: number) => {
const processDownloaded = async(bytes: Uint8Array) => {
if(process) {
//const perf = performance.now();
const processed = await process(bytes, fileName);
@ -473,13 +473,18 @@ export class ApiFileManager { @@ -473,13 +473,18 @@ export class ApiFileManager {
deferred.notify({done, offset, total: size});
//}
const processedResult = await processDownloaded(bytes, offset);
await writeFilePromise;
checkCancel();
await writeFilePromise;
await fileWriter.write(bytes, offset);
}
if(isFinal && process) {
const bytes = fileWriter.getParts();
const processedResult = await processDownloaded(bytes);
checkCancel();
await fileManager.write(fileWriter, processedResult);
fileWriter.replaceParts(processedResult);
}
writeFileDeferred.resolve();
@ -487,7 +492,12 @@ export class ApiFileManager { @@ -487,7 +492,12 @@ export class ApiFileManager {
if(isFinal) {
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) {
errorHandler(err as Error);

2
src/lib/rlottie/lottieLoader.ts

@ -101,7 +101,7 @@ export class LottieLoader { @@ -101,7 +101,7 @@ export class LottieLoader {
return fetch(url)
.then(res => {
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 {
return res.blob();
}

Loading…
Cancel
Save