supabase / storage-js

JS Client library to interact with Supabase Storage
Apache License 2.0
129 stars 39 forks source link

Add function to get filePath from publicUrl #74

Open DriesCruyskens opened 2 years ago

DriesCruyskens commented 2 years ago

Feature request

Is your feature request related to a problem? Please describe.

The storageFileApi class has a function to convert file paths to public url's. These urls are very simple and just concatenate the file path with some fixed strings. When deleting a file however, you need the path again.

Describe the solution you'd like

Instead of having to save the file path separately or converting the public url to a path manually before removing a file. I suggest implementing a function that converts the public url back to a file path.

Describe alternatives you've considered

Right now I manually convert the public url to the path again using

final filePath = publicUrl.replaceAll('${SupabaseClient.storageUrl}/object/public/avatars/', '');

so it can be used when deleting the file

SupabaseClient.storage.from('avatars').remove([filePath]);

but I feel this can be easily built in.

Solution

it can be as simple as adding this function which is just the getPublicUrl() function but in reverse.

StorageResponse<String> getPathFromPublicUrl(String publicUrl) {
    try {
      final filePath = publicUrl.replaceAll('${SupabaseClient.storageUrl}/object/public/$bucketId/', '');
      return StorageResponse<String>(data: filePath);
    } catch (e) {
      return StorageResponse(error: StorageError(e.toString()));
    }
  }
tonyneel commented 2 years ago

I think this is a very common use case and would be helpful to have built

madebyfabian commented 1 year ago

+1

abdirahmn1 commented 11 months ago

+1

pierroo commented 7 months ago

Do you guys know if it was added since?

kingraphaii commented 6 months ago

+1

beachstrider commented 3 months ago

+1