0

I have a flutter app which uses firebase/gcp as a backend. Files can be uploaded and stored on google cloud storage.

In flutter I am using an UploadImage and UploadVideo model to store information like path and url for these files.

Example:

@freezed
class UploadImage with _$UploadImage {
  const UploadImage({required String url, required String id, required String path});
  
  factory UploadImage.fromJson(Map<String, dynamic> json) => _$UploadImageFromJson(json);
}

// Usage
@freezed
class User with _$User {
  const User({
    //...
    UploadImage? profilePicture,
  });

  // ... fromJson
}

The app then references / displays the images using their url property, like:


final image = UploadImage(url: "test-image-url");
return CachedNetworkImage(imageUrl: image.url);

I have now added a CDN for the media files and have to replace the image/videos tld with the CDN tld. What is the best solution to do this? I have though about:

  1. Using a service to get the image url from the object but this would require to change the code everywhere the image is used.
  2. Use fromJson to alter the URL when json data is parsed but this seems like a anti pattern and still allows instantiation with "invalid" urls.

but none of these solutions sound good.

Do you have any recomendations, design patterns or tips on how to achieve this task?

EDIT
URLs for files are generated using firebase getDownloadURL() and are not served over the apps domain but directly from google cloud storage. DownloadURL looks something like:

https://firebasestorage.googleapis.com/v0/b/${FIREBASE_PROJECT_ID}.appspot.com/o/${URL_ENCODED_PATH_TO_FILE}?alt=media&token=${ACCESS_TOKEN}

Thats also the reason why I am unable to just point domain to for example cloudflare because the images are not served over my controlled domains. But I am also storing the path for each file so to generate the CDNs file URL I do ${CDN_HOST}/${PATH_TO_FILE}. Ive followed this guide to setup a loadbalancer with CDN on GCP: https://cloud.google.com/load-balancing/docs/https/ext-load-balancer-backend-buckets

3
  • Can you clarify what these URLs look like? For example, is your main site www.example.com but you need static assets like images to come from, say, media.example.com? May 3 at 12:17
  • A content delivery network should be a transparent component in your architecture. Typically you would point www.example.com to the CDN servers, and configure the CDN so it properly caches static assets, but passes requests for dynamic resources on to your servers. May 3 at 12:19
  • @GregBurghardt Ive added the url formats and details about the loadbalancer to the question. May 3 at 12:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.