Skip to content

Fix truncating urls for paths with multiple dots (#46) #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/url/__tests__/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ describe('Url', () => {
it('should return as is', () => {
expect(extractPublicId('example')).toEqual('example')
})

it('should correctly handle filenames with no extension', () => {
expect(extractPublicId('https://res.cloudinary.com/xyz/image/upload/v111111111/Folder/noextension')).toBe('Folder/noextension')
})

it('should allow multiple dots in the filename before the extension', () => {
expect(extractPublicId('https://res.cloudinary.com/xyz/image/upload/v111111111/Folder/Name.With.Multiple.Dots.png')).toBe('Folder/Name.With.Multiple.Dots')
})
})

describe('getSubDomain', () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/url/lib/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import { toTransformationStr, transform } from './transformers'
const SHARED_CDNS:string[] = ["cloudinary-a.akamaihd.net", "res.cloudinary.com"]

//eslint-disable-next-line
const CLOUDINARY_REGEX = /^.+\.cloudinary\.com\/(?:[^\/]+\/)(?:(image|video|raw)\/)?(?:(upload|fetch|private|authenticated|sprite|facebook|twitter|youtube|vimeo)\/)?(?:(?:[^_/]+_[^,/]+,?)*\/)?(?:v(\d+|\w{1,2})\/)?([^\.^\s]+)(?:\.(.+))?$/
const CLOUDINARY_REGEX = /^.+\.cloudinary\.com\/(?:[^\/]+\/)(?:(image|video|raw)\/)?(?:(upload|fetch|private|authenticated|sprite|facebook|twitter|youtube|vimeo)\/)?(?:(?:[^_/]+_[^,/]+,?)*\/)?(?:v(\d+|\w{1,2})\/)?(.+)$/

export const extractPublicId = (link: string):string => {
if (!link) return ''

const parts = CLOUDINARY_REGEX.exec(link)

return parts && parts.length > 2 ? parts[parts.length - 2] : link
if (!parts) {
return link
}

// Strip extension if present
return parts[parts.length - 1].replace(/\.[^/.]+$/, "")
}

export const getSignature = (signature?: string) => {
Expand Down