Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Louis Ferreira 5 posts 73 karma points
    Jan 30, 2024 @ 13:58
    Louis Ferreira
    0

    Need help to figure out a way to 'Publish' and 'Unpublish' Media item

    I know this title sounds a little weird, but hear me out...

    I have a client that wants to link to (downloadable) media item(s) from a page, but the media item is time and date sensitive (e.g. a pdf file will only be available to download for a limited time).

    So I need a way that I can sort of publish and unpublish the media item in a similar way to content nodes (either manually or scheduled). I'm not even sure where to begin with this, so looking for any suggestions or pointers with this one.

    I still need the client to clarify what happens when a user clicks on a link to an 'unpublished' media item (silently fail the download, return 404, etc) but in the meantime, I'd like to make a start on somehow restricting the media item to be only available within the allotted time frame.

    p.s. I've looked at Media Protect package, and it's not really what is required here.

    My thoughts so far:

    1. HttpHandler
    2. HttpModule
    3. Custom implementation of IMediaUrlProvider
  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Feb 02, 2024 @ 11:51
    Marc Goodson
    0

    Hi Louis

    A lot depends on the context of this, and how often this sort of page and download functionality occurs, eg 20 times a day this gets setup or if there is a new file download, or once every three months.

    Also, once the period is over what should happen to the Media Item?, eg can it be deleted, or will it need to stay there for the next time it will be used etc...

    ... so creating the notion of published/unpublished media may solve the problem, in which case you want a HttpModule, as you'll want to intercept all requests to /media and then look to see if the media item is currently embargoed. With this approach though you want to avoid 'trying to find the Media Item in Umbraco based on it's Url' - because this will slow down all requests to media on the site... if you take this approach, then when a Media Item is Saved in Umbraco you'll want to store handle that saving event, and store it's published Url, and start end dates, into something that is fast to lookup, eg Redis etc...

    This media authorisation package, has the general gist, of hooking into the request and seeing if it is to a media folder etc, that might give a steer...

    https://github.com/Moriyama-Umbraco/Moriyama-Authorised-Media/blob/master/src/Moriyama.AuthorisedMedia/Application/AuthorisedMediaHttpModule.cs

    But, the other thought, which is a bit more clunky perhaps, is to use a Content Item in the Content Tree, which has a Media Picker, or path to the file somewhere outside of Umbraco.

    This Content Item can then be picked on a page to show/hide the link to the download, but because it is, content it can be scheduled to be published... eg you can pick it before it is published, and it's scheduled publish makes it appear! - you could schedule it to be unpublished, too, and although that would hide the download link automatically (as you would write code to see if the picked content item is published or not), the problem would be the Media item would be still available on it's direct Url.

    Before it is published this isn't necessarily a problem because nobody would know the Media Items direct Url until it's published (unless this is a repeat of an earlier publish).

    You could handle the Event thrown when the Cache is updated on a server: https://our.umbraco.com/Documentation/Reference/Events/CacheRefresher-Events#usage and look for a RemoveById message, and see if this Id is one of the wrapping content items, and if so, find the related Media Item and delete it/rename it, preventing it from being downloaded...

    ... I did say it was clunky!

    regards

    Marc

  • Louis Ferreira 5 posts 73 karma points
    Feb 02, 2024 @ 14:54
    Louis Ferreira
    0

    Hi Marc,

    These are all great questions that I've already asked my client. And the short answer is:- high volume and short duration, and that the media file doesn't have to remain on the server post embargo. And if a link to a previously available item is book-marked or saved and then is requested, then a 404 page/code is returned to the browser.

    Your link to HttpModule (and Paul's HttpHandler on codeshare.co.uk) is kinda where I'm heading with this, as the publish/unpublish is proving to be a big ask at this stage (I'd have to re-write a lot of Umbraco core code to do that). But very good shout on caching the id of new media items to prevent performance degradation for other media! H5!

    Your 'clunky' approach does have the drawback in that when someone bookmarks or shares a link directly to the media item it would still be downloadable, so that's not really a viable option... but combining it with that CacheRefresher like you mentioned, might be an avenue to explore!

    Appreciate your input on this Marc, Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft