Skip to content

Commit 6c0f2a9

Browse files
authored
Refactor ExportedBlob.copyFrom (#8213)
1 parent 596ff08 commit 6c0f2a9

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

app/lib/package/api_export/exported_api.dart

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -450,38 +450,43 @@ final class ExportedBlob extends ExportedObject {
450450
}));
451451
}
452452

453-
/// Copy binary blob from [bucket] and [source] to this file.
454-
Future<void> copyFrom(Bucket bucket, String source) async {
453+
/// Copy binary blob from [absoluteSourceObjectName] to this file.
454+
///
455+
/// Requires that [absoluteSourceObjectName] is a `gs:/<bucket>/<objectName>`
456+
/// style URL. These can be created with [Bucket.absoluteObjectName].
457+
///
458+
/// [sourceInfo] is required to be [ObjectInfo] for the source object.
459+
/// This method will use [ObjectInfo.length] and [ObjectInfo.md5Hash] to
460+
/// determine if it's necessary to copy the object.
461+
Future<void> copyFrom(
462+
String absoluteSourceObjectName,
463+
ObjectInfo sourceInfo,
464+
) async {
455465
final metadata = _metadata();
456-
Future<ObjectInfo?>? srcInfo;
457466

458467
await Future.wait(_owner._prefixes.map((prefix) async {
459468
await _owner._pool.withResource(() async {
460469
final dst = prefix + _objectName;
461470

462471
// Check if the dst already exists
463472
if (await _owner._bucket.tryInfo(dst) case final dstInfo?) {
464-
// Fetch info for source object (if we haven't already done this)
465-
srcInfo ??= bucket.tryInfo(source);
466-
if (await srcInfo case final srcInfo?) {
467-
if (dstInfo.contentEquals(srcInfo)) {
468-
// If both source and dst exists, and their content matches, then
469-
// we only need to update the "validated" metadata. And we only
470-
// need to update the "validated" timestamp if it's older than
471-
// _retouchDeadline
472-
final retouchDeadline = clock.agoBy(_updateValidatedAfter);
473-
if (dstInfo.metadata.validated.isBefore(retouchDeadline)) {
474-
await _owner._bucket.updateMetadata(dst, metadata);
475-
}
476-
return;
473+
if (dstInfo.contentEquals(sourceInfo)) {
474+
// If both source and dst exists, and their content matches, then
475+
// we only need to update the "validated" metadata. And we only
476+
// need to update the "validated" timestamp if it's older than
477+
// _retouchDeadline
478+
final retouchDeadline = clock.agoBy(_updateValidatedAfter);
479+
if (dstInfo.metadata.validated.isBefore(retouchDeadline)) {
480+
await _owner._bucket.updateMetadata(dst, metadata);
477481
}
482+
return;
478483
}
479484
}
480485

481486
// If dst or source doesn't exist, then we shall attempt to make a copy.
482487
// (if source doesn't exist we'll consistently get an error from here!)
483488
await _owner._storage.copyObject(
484-
bucket.absoluteObjectName(source),
489+
absoluteSourceObjectName,
485490
_owner._bucket.absoluteObjectName(dst),
486491
metadata: metadata,
487492
);

app/test/package/api_export/exported_api_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ void main() {
105105
await exportedApi.package('retry').tarball('1.2.3').write([1, 2, 3]);
106106

107107
await exportedApi.package('retry').tarball('1.2.4').copyFrom(
108-
bucket,
109-
'latest/api/archives/retry-1.2.3.tar.gz',
108+
bucket.absoluteObjectName('latest/api/archives/retry-1.2.3.tar.gz'),
109+
await bucket.info('latest/api/archives/retry-1.2.3.tar.gz'),
110110
);
111111

112112
// Files are present

0 commit comments

Comments
 (0)