Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit c8028d3

Browse files
committed
Address PR feedback.
1 parent 6caa5e6 commit c8028d3

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

packages/google_sign_in/google_sign_in/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## 4.0.13
22

3-
* Fix GoogleUserCircleAvatar to handle new style profile image URLs.
3+
* Fix `GoogleUserCircleAvatar` to handle new style profile image URLs.
44

55
## 4.0.12
66

packages/google_sign_in/lib/src/fife.dart

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,58 @@
1+
// Copyright 2019 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
15
/// A regular expression that matches against the "size directive" path
26
/// segment of Google profile image URLs.
37
///
48
/// The format is is "`/sNN-c/`", where `NN` is the max width/height of the
59
/// image, and "`c`" indicates we want the image cropped.
610
final RegExp sizeDirective = RegExp(r'^s[0-9]{1,5}(-c)?$');
711

8-
/// Adds [size] directive to [photoUrl].
12+
/// Adds [size] (and crop) directive to [photoUrl].
13+
///
14+
/// There are two formats for photoUrls coming from the Sign In backend.
15+
///
16+
/// The two formats can be told apart by the number of path segments in the
17+
/// URL (path segments: parts of the URL separated by slashes "/"):
18+
///
19+
/// * If the URL has 2 or less path segments, it is a *new* style URL.
20+
/// * If the URL has more than 2 path segments, it is an old style URL.
21+
///
22+
/// Old style URLs encode the image transformation directives as the last
23+
/// path segment. Look at the [sizeDirective] Regular Expression for more
24+
/// information about these URLs.
925
///
10-
/// Depending on the format of photoUrl, this might:
11-
/// * Insert information as the last path segment (old style URLs), before
12-
/// the image filename. The format is described in [sizeDirective].
13-
/// * Insert information at the end of the URL, after an = sign. The format
14-
/// is described within this method.
26+
/// New style URLs carry the same directives at the end of the URL,
27+
/// after an = sign, like: "`=s120-c-fSoften=1,50,0`".
28+
///
29+
/// Directives may contain the "=" sign (`fSoften=1,50,0`), but it seems the
30+
/// base URL of the images don't. "Everything after the first = sign" is a
31+
/// good heuristic to split new style URLs.
32+
///
33+
/// Each directive is separated from others by dashes. Directives are the same
34+
/// as described in the [sizeDirective] RegExp.
35+
///
36+
/// Modified image URLs are recomposed by performing the parsing steps in reverse.
1537
String addSizeDirectiveToUrl(String photoUrl, double size) {
1638
final Uri profileUri = Uri.parse(photoUrl);
1739
final List<String> pathSegments = List<String>.from(profileUri.pathSegments);
1840
if (pathSegments.length <= 2) {
19-
/// New URLs may have directives at the end of the URL, like "`=sNN-c`".
20-
/// Each filter is separated by dashes. Filters may contain = signs:
21-
/// "`=s120-c-fSoften=1,50,0`"
2241
final String imagePath = pathSegments.last;
23-
// Locate the first =
42+
// Does this have any existing transformation directives?
2443
final int directiveSeparator = imagePath.indexOf('=');
2544
if (directiveSeparator >= 0) {
26-
// Split the image URL by the first =
27-
final String image = imagePath.substring(0, directiveSeparator);
45+
// Split the baseUrl from the sizing directive by the first "="
46+
final String baseUrl = imagePath.substring(0, directiveSeparator);
2847
final String directive = imagePath.substring(directiveSeparator + 1);
29-
// Split the second half by -
48+
// Split the directive by "-"
3049
final Set<String> directives = Set<String>.from(directive.split('-'))
31-
// Remove the size directive, if present, and empty values
50+
// Remove the size directive, if present, and any empty values
3251
..removeWhere((String s) => s.isEmpty || sizeDirective.hasMatch(s))
3352
// Add the size and crop directives
3453
..addAll(<String>['c', 's${size.round()}']);
35-
36-
pathSegments.last = '$image=${directives.join("-")}';
54+
// Recompose the URL by performing the reverse of the parsing
55+
pathSegments.last = '$baseUrl=${directives.join("-")}';
3756
} else {
3857
pathSegments.last = '${pathSegments.last}=c-s${size.round()}';
3958
}

0 commit comments

Comments
 (0)