Nicolas Raoul
Nicolas Raoul

Reputation: 60213

Deconstructing a Document and Media download URL

I want to keep track of all files downloaded from Document and Media, so I wrote a Servlet Filter that gets URLs when a file is downloaded. Depending from where in the UI the file is downloaded, it can look like these:

http://s/documents/20143/32701/SomeFile.txt/da99a46a-8231-2a87-ff8d-8d3b2d388c24
http://s/documents/20143/0/invoice_ABC.pdf/c44fd479-331b-f393-7879-973c5cecf086

In my audit I would like to write down details about the downloaded file, so I need to extract info from that URL.

What is each part of the URL for?

Note: I know that relying on these parameters in this order is dangerous, but when doing audit I don't think there is any better option, as the Java code behind download can not be overridden by an OSGi module.

Upvotes: 1

Views: 1547

Answers (1)

Nicolas Raoul
Nicolas Raoul

Reputation: 60213

  • http://s: Your protocol and server name, obviously.
  • documents: A keyword, it is always the same.

What follows is a path with up to four parts. Depending on the count of parts, they can have different meanings:

  • http://s/documents/80327

    • 80327: The ID of a FileShortcut (DLFileShortcut.fileShortcutId)
    • The file entry may be found with: DLAppServiceUtil.getFileEntry(DLAppServiceUtil.getFileShortcut(80327).getToFileEntryId())
  • http://s/documents/20143/da99a46a-8231-2a87-ff8d-8d3b2d388c24

    • 20143: The ID of the site/group to which the file entry belongs (DLFileEntry.groupId)
    • da99a46a-8231-2a87-ff8d-8d3b2d388c24: The UUID of the file entry (DLFileEntry.uuid)
    • The file entry may be found with: DLAppServiceUtil.getFileEntryByUuidAndGroupId("da99a46a-8231-2a87-ff8d-8d3b2d388c24", 20143)
  • http://s/documents/20143/32701/SomeFile.txt

    • 20143: The ID of the site/group to which the file entry belongs (DLFileEntry.groupId)
    • 32701: The ID of the folder to which the file entry belongs (DLFileEntry.folderId)
    • SomeFile.txt: The file's name, with special characters encoded as URL escape sequences (DLFileEntry.name)
    • The file entry may be found with: DLAppServiceUtil.getFileEntry(20143, 32701, HttpUtil.decodeUrl("SomeFile.txt"))
  • http://s/documents/20143/32701/SomeFile.txt/da99a46a-8231-2a87-ff8d-8d3b2d388c24

    • 20143: The ID of the site/group to which the file entry belongs (DLFileEntry.groupId)
    • 32701: The ID of the folder to which the file entry belongs - ignored
    • SomeFile.txt: The file's name - ignored
    • da99a46a-8231-2a87-ff8d-8d3b2d388c24: The UUID of the file entry. (DLFileEntry.uuid)
    • The file entry may be found with: DLAppServiceUtil.getFileEntryByUuidAndGroupId("da99a46a-8231-2a87-ff8d-8d3b2d388c24", 20143)

Liferay usually generates URLs of the last format to have a more or less "friendly" URL which is hardened against moving or renaming of files.

A notable absent is the company id, it can be retrieved from DLFileEntry.companyId or Group.companyId.

Warning: The elements and their order might change in the future. The current implementation can be found in com.liferay.portal.webserver.WebServerServlet.getFileEntry(String[] pathArray).

This is a community wiki answer, so feel free to edit it to add/fix information.

Upvotes: 1

Related Questions