fix: upload status to table

This commit is contained in:
Prateek Sunal 2024-04-10 20:31:04 +05:30
parent a45092931d
commit ce3a3bd802
3 changed files with 110 additions and 2 deletions

View file

@ -3,6 +3,7 @@ import 'dart:io';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import "package:photos/core/constants.dart";
import "package:photos/utils/multipart_upload_util.dart";
import 'package:sqflite/sqflite.dart';
@ -31,6 +32,11 @@ class UploadLocksDB {
columnPartSize: "part_size",
);
static const _trackStatus = (
pending: "pending",
completed: "completed",
);
static const _partsTable = (
table: "upload_parts",
columnObjectKey: "object_key",
@ -38,6 +44,10 @@ class UploadLocksDB {
columnPartUrl: "part_url",
columnPartStatus: "part_status",
);
static const _partStatus = (
pending: "pending",
uploaded: "uploaded",
);
UploadLocksDB._privateConstructor();
static final UploadLocksDB instance = UploadLocksDB._privateConstructor();
@ -210,4 +220,74 @@ class UploadLocksDB {
return urls;
}
Future<void> createTrackUploadsEntry(
String localId,
String fileHash,
MultipartUploadURLs urls,
String encryptedFilePath,
int fileSize,
) async {
final db = await UploadLocksDB.instance.database;
final objectKey = urls.objectKey;
await db.insert(
_trackUploadTable.table,
{
_trackUploadTable.columnLocalID: localId,
_trackUploadTable.columnFileHash: fileHash,
_trackUploadTable.columnObjectKey: objectKey,
_trackUploadTable.columnCompleteUrl: urls.completeURL,
_trackUploadTable.columnEncryptedFilePath: encryptedFilePath,
_trackUploadTable.columnEncryptedFileSize: fileSize,
_trackUploadTable.columnCompletionStatus: _trackStatus.pending,
_trackUploadTable.columnPartSize: multipartPartSize,
},
);
final partsURLs = urls.partsURLs;
final partsLength = partsURLs.length;
for (int i = 0; i < partsLength; i++) {
await db.insert(
_partsTable.table,
{
_partsTable.columnObjectKey: objectKey,
_partsTable.columnPartNumber: i,
_partsTable.columnPartUrl: partsURLs[i],
_partsTable.columnPartStatus: _partStatus.pending,
},
);
}
}
Future<void> updatePartStatus(
String objectKey,
int partNumber,
) async {
final db = await instance.database;
await db.update(
_partsTable.table,
{
_partsTable.columnPartStatus: _partStatus.uploaded,
},
where:
'${_partsTable.columnObjectKey} = ? AND ${_partsTable.columnPartNumber} = ?',
whereArgs: [objectKey, partNumber],
);
}
Future<void> updateCompletionStatus(
String objectKey,
) async {
final db = await instance.database;
await db.update(
_trackUploadTable.table,
{
_trackUploadTable.columnCompletionStatus: _trackStatus.completed,
},
where: '${_trackUploadTable.columnObjectKey} = ?',
whereArgs: [objectKey],
);
}
}

View file

@ -515,6 +515,14 @@ class FileUploader {
);
} else {
final fileUploadURLs = await getMultipartUploadURLs(count);
await createTableEntry(
lockKey,
mediaUploadData.hashData!.fileHash!,
fileUploadURLs,
encryptedFilePath,
await encryptedFile.length(),
);
fileObjectKey = await putMultipartFile(fileUploadURLs, encryptedFile);
}
}

View file

@ -73,6 +73,22 @@ Future<MultipartUploadURLs> getMultipartUploadURLs(int count) async {
}
}
Future<void> createTableEntry(
String localId,
String fileHash,
MultipartUploadURLs urls,
String encryptedFilePath,
int fileSize,
) async {
await UploadLocksDB.instance.createTrackUploadsEntry(
localId,
fileHash,
urls,
encryptedFilePath,
fileSize,
);
}
Future<String> putExistingMultipartFile(
File encryptedFile,
String localId,
@ -84,7 +100,7 @@ Future<String> putExistingMultipartFile(
final etags = await uploadParts(urls, encryptedFile);
// complete the multipart upload
await completeMultipartUpload(etags, urls.completeURL);
await completeMultipartUpload(urls.objectKey, etags, urls.completeURL);
return urls.objectKey;
}
@ -97,7 +113,7 @@ Future<String> putMultipartFile(
final etags = await uploadParts(urls, encryptedFile);
// complete the multipart upload
await completeMultipartUpload(etags, urls.completeURL);
await completeMultipartUpload(urls.objectKey, etags, urls.completeURL);
return urls.objectKey;
}
@ -141,12 +157,15 @@ Future<Map<int, String>> uploadParts(
}
etags[i] = eTag!;
await UploadLocksDB.instance.updatePartStatus(url.objectKey, i);
}
return etags;
}
Future<void> completeMultipartUpload(
String objectKey,
Map<int, String> partEtags,
String completeURL,
) async {
@ -169,6 +188,7 @@ Future<void> completeMultipartUpload(
contentType: "text/xml",
),
);
await UploadLocksDB.instance.updateCompletionStatus(objectKey);
} catch (e) {
Logger("MultipartUpload").severe(e);
rethrow;