mirror of
https://github.com/twonlyapp/twonly-app.git
synced 2026-01-15 11:18:41 +00:00
fixed some issues with the migration
This commit is contained in:
parent
80d6f85350
commit
d59b8602f9
3 changed files with 103 additions and 129 deletions
|
|
@ -1,7 +1,6 @@
|
|||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'package:camera/camera.dart';
|
||||
import 'package:drift/drift.dart' show Value;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_android_volume_keydown/flutter_android_volume_keydown.dart';
|
||||
|
|
|
|||
|
|
@ -218,7 +218,16 @@ class _UserListItem extends State<GroupListItem> {
|
|||
subtitle: (_currentMessage == null)
|
||||
? (widget.group.totalMediaCounter == 0)
|
||||
? Text(context.lang.chatsTapToSend)
|
||||
: LastMessageTime(dateTime: widget.group.lastMessageExchange)
|
||||
: Row(
|
||||
children: [
|
||||
LastMessageTime(
|
||||
dateTime: widget.group.lastMessageExchange),
|
||||
FlameCounterWidget(
|
||||
groupId: widget.group.groupId,
|
||||
prefix: true,
|
||||
),
|
||||
],
|
||||
)
|
||||
: Row(
|
||||
children: [
|
||||
MessageSendStateIcon(
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import 'dart:collection' show HashSet;
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:cryptography_plus/cryptography_plus.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:path/path.dart';
|
||||
|
|
@ -8,13 +10,11 @@ import 'package:restart_app/restart_app.dart';
|
|||
import 'package:twonly/globals.dart';
|
||||
import 'package:twonly/src/database/daos/contacts.dao.dart';
|
||||
import 'package:twonly/src/database/tables/mediafiles.table.dart';
|
||||
import 'package:twonly/src/database/tables/messages.table.dart';
|
||||
import 'package:twonly/src/database/twonly.db.dart';
|
||||
import 'package:twonly/src/database/twonly_database_old.dart'
|
||||
show TwonlyDatabaseOld;
|
||||
import 'package:twonly/src/services/mediafiles/mediafile.service.dart';
|
||||
import 'package:twonly/src/utils/log.dart';
|
||||
import 'package:twonly/src/utils/misc.dart';
|
||||
import 'package:twonly/src/utils/storage.dart';
|
||||
|
||||
class DatabaseMigrationView extends StatefulWidget {
|
||||
|
|
@ -37,14 +37,14 @@ class _DatabaseMigrationViewState extends State<DatabaseMigrationView> {
|
|||
|
||||
final oldDatabase = TwonlyDatabaseOld();
|
||||
final oldContacts = await oldDatabase.contacts.select().get();
|
||||
final oldMessages = await oldDatabase.messages.select().get();
|
||||
|
||||
for (final oldContact in oldContacts) {
|
||||
try {
|
||||
if (oldContact.deleted) continue;
|
||||
Uint8List? avatarSvg;
|
||||
if (oldContact.avatarSvg != null) {
|
||||
avatarSvg =
|
||||
Uint8List.fromList(gzip.encode(utf8.encode(oldContact.avatarSvg!)));
|
||||
avatarSvg = Uint8List.fromList(
|
||||
gzip.encode(utf8.encode(oldContact.avatarSvg!)));
|
||||
}
|
||||
await twonlyDB.contactsDao.insertContact(
|
||||
ContactsCompanion(
|
||||
|
|
@ -64,7 +64,7 @@ class _DatabaseMigrationViewState extends State<DatabaseMigrationView> {
|
|||
setState(() {
|
||||
_contactsMigrated += 1;
|
||||
});
|
||||
final group = await twonlyDB.groupsDao.createNewDirectChat(
|
||||
await twonlyDB.groupsDao.createNewDirectChat(
|
||||
oldContact.userId,
|
||||
GroupsCompanion(
|
||||
pinned: Value(oldContact.pinned),
|
||||
|
|
@ -79,86 +79,48 @@ class _DatabaseMigrationViewState extends State<DatabaseMigrationView> {
|
|||
lastMessageReceived: Value(oldContact.lastMessageReceived),
|
||||
lastMessageSend: Value(oldContact.lastMessageSend),
|
||||
flameCounter: Value(oldContact.flameCounter),
|
||||
maxFlameCounter: Value(oldContact.flameCounter),
|
||||
maxFlameCounterFrom: Value(DateTime.now()),
|
||||
),
|
||||
);
|
||||
if (group == null) continue;
|
||||
for (final oldMessage in oldMessages) {
|
||||
if (oldMessage.mediaUploadId == null &&
|
||||
oldMessage.mediaDownloadId == null) {
|
||||
/// only interested in media files...
|
||||
continue;
|
||||
}
|
||||
if (oldMessage.contactId != oldContact.userId) continue;
|
||||
if (!oldMessage.mediaStored) continue;
|
||||
|
||||
var storedMediaPath =
|
||||
join((await getApplicationSupportDirectory()).path, 'media');
|
||||
if (oldMessage.mediaDownloadId != null) {
|
||||
storedMediaPath =
|
||||
'${join(storedMediaPath, 'received')}/${oldMessage.mediaDownloadId}';
|
||||
} else {
|
||||
storedMediaPath =
|
||||
'${join(storedMediaPath, 'send')}/${oldMessage.mediaDownloadId}';
|
||||
}
|
||||
|
||||
var type = MediaType.image;
|
||||
if (File('$storedMediaPath.mp4').existsSync()) {
|
||||
type = MediaType.video;
|
||||
storedMediaPath = '$storedMediaPath.mp4';
|
||||
} else if (File('$storedMediaPath.png').existsSync()) {
|
||||
type = MediaType.image;
|
||||
storedMediaPath = '$storedMediaPath.png';
|
||||
} else if (File('$storedMediaPath.webp').existsSync()) {
|
||||
type = MediaType.image;
|
||||
storedMediaPath = '$storedMediaPath.webp';
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
final uniqueId = Value(
|
||||
getUUIDforDirectChat(
|
||||
oldMessage.messageOtherId ?? oldMessage.messageId,
|
||||
oldMessage.contactId ^ gUser.userId,
|
||||
),
|
||||
);
|
||||
|
||||
final mediaFile = await twonlyDB.mediaFilesDao.insertMedia(
|
||||
MediaFilesCompanion(
|
||||
mediaId: uniqueId,
|
||||
stored: const Value(true),
|
||||
type: Value(type),
|
||||
createdAt: Value(oldMessage.sendAt),
|
||||
),
|
||||
);
|
||||
if (mediaFile == null) continue;
|
||||
|
||||
final message = await twonlyDB.messagesDao.insertMessage(
|
||||
MessagesCompanion(
|
||||
messageId: uniqueId,
|
||||
groupId: Value(group.groupId),
|
||||
mediaId: uniqueId,
|
||||
type: const Value(MessageType.media),
|
||||
),
|
||||
);
|
||||
if (message == null) continue;
|
||||
|
||||
final mediaService = await MediaFileService.fromMedia(mediaFile);
|
||||
File(storedMediaPath).copySync(mediaService.storedPath.path);
|
||||
setState(() {
|
||||
_storedMediaFiles += 1;
|
||||
});
|
||||
} catch (e) {
|
||||
Log.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
final folders = ['memories', 'send', 'received'];
|
||||
|
||||
final alreadyCopied = HashSet();
|
||||
|
||||
for (final folder in folders) {
|
||||
final memoriesPath = Directory(
|
||||
join((await getApplicationSupportDirectory()).path, 'media', 'memories'),
|
||||
join(
|
||||
(await getApplicationSupportDirectory()).path,
|
||||
'media',
|
||||
folder,
|
||||
),
|
||||
);
|
||||
if (memoriesPath.existsSync()) {
|
||||
final files = memoriesPath.listSync();
|
||||
for (final file in files) {
|
||||
try {
|
||||
if (file.path.contains('thumbnail')) continue;
|
||||
final type =
|
||||
file.path.contains('mp4') ? MediaType.video : MediaType.image;
|
||||
late MediaType type;
|
||||
if (file.path.contains('mp4')) {
|
||||
type = MediaType.video;
|
||||
} else if (file.path.contains('png')) {
|
||||
type = MediaType.image;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
final bytes = File(file.path).readAsBytesSync();
|
||||
final digest = (await Sha256().hash(bytes)).bytes;
|
||||
if (alreadyCopied.contains(digest)) {
|
||||
continue;
|
||||
}
|
||||
alreadyCopied.add(digest);
|
||||
|
||||
final stat = FileStat.statSync(file.path);
|
||||
final mediaFile = await twonlyDB.mediaFilesDao.insertMedia(
|
||||
MediaFilesCompanion(
|
||||
|
|
@ -172,6 +134,10 @@ class _DatabaseMigrationViewState extends State<DatabaseMigrationView> {
|
|||
setState(() {
|
||||
_storedMediaFiles += 1;
|
||||
});
|
||||
} catch (e) {
|
||||
Log.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue