Skip to content

Commit

Permalink
LDEV-4949 serve extensions directly for older versions
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Jun 26, 2024
1 parent 04c54ff commit da635cb
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
3 changes: 3 additions & 0 deletions apps/updateserver/Application.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ component {
, mavenMatcher = new services.legacy.MavenMatcher()
);

var extensionCache = new services.ExtensionCache();

extMetaReader.setBundleDownloadservice( bundleDownloadService );
extMetaReader.loadMeta();

Expand All @@ -83,6 +85,7 @@ component {
application.extensionsCdnUrl = extCdnUrl;
application.extensionsS3Root = extS3Root;
application.extMetaReader = extMetaReader;
application.extensionCache = extensionCache;
application.bundleDownloadService = bundleDownloadService;
application.jiraChangelogService = jiraChangelogService;
}
Expand Down
23 changes: 21 additions & 2 deletions apps/updateserver/rest/ExtensionProvider.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
*/
component {

variables.metaReader = application.extMetaReader;
variables.cdnURL = application.extensionsCdnUrl;
variables.metaReader = application.extMetaReader;
variables.cdnURL = application.extensionsCdnUrl;
variables.extensionCache = application.extensionCache

/**
* @httpmethod GET
Expand Down Expand Up @@ -91,7 +92,25 @@ component {
, withLogo = false
);

param name="url.allowRedirect" default="";
if ( isEmpty( url.allowRedirect ) && left( cgi.request_url, 5 ) == "http:" ){
url.allowRedirect = false; // fall back to serving directly for older versions
} else {
url.allowRedirect = true;
}

if ( StructCount( ext ) ) {
if ( !url.allowRedirect ){
var path = extensionCache.getExtensionLex( variables.cdnURL & ext.filename );
var filename = listLast( path, "/" );
header name="cache-control" value="public, max-age=#DateDiff( "s", Now(), expires )#";
header name="Content-Disposition" value="attachment; filename=""#fileName#""";
content
reset = true
file = path
type = "application/x-zip-compressed"
deletefile = false;
}
header statuscode="302" statustext="Found";
header name="Location" value=variables.cdnURL & ext.filename;
return;
Expand Down
36 changes: 36 additions & 0 deletions apps/updateserver/services/ExtensionCache.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
component accessors=true {

property name="extensionCache" type="any" default="cache/extensions/";

function getExtensionLex( extensionUrl ){
var filename = listLast( arguments.extensionUrl, "/");
var cachedir = expandPath( variables.extensionCache );

if ( !directoryExists( cachedir ) ){
directoryCreate( cachedir );
} else if ( fileExists ( cachedir & filename )) {
return cachedir & filename;
}
lock name="ext-#filename#" type="exclusive" timeout=10 {
return _fetchExtensionLex( cachedir, arguments.extensionUrl )
}

if ( fileExists ( cachedir & filename ) ) {
return cachedir & filename;
}
throw "unable to find extension [#filename#]";
}

private function _fetchExtensionLex( string cachedir, string extensionUrl ){
var filename = listLast( arguments.extensionUrl, "/");
if ( fileExists ( cachedir & filename )) {
return cachedir & filename;
}
systemOutput( "Downloading #arguments.extensionUrl# (cache miss)", true);
http url=arguments.extensionUrl method="get" result="local.core" path=cachedir;
var file = directoryList(path=cachedir, filter="#filename#");
// systemOutput(file, true);
return file[ 1 ];
}

}
38 changes: 38 additions & 0 deletions tests/testExtensionDownload.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
component extends="org.lucee.cfml.test.LuceeTestCase" labels="data-provider-integration" {

function beforeAll (){
variables.dir = getDirectoryFromPath(getCurrentTemplatePath());
application action="update" mappings={
"/services" : expandPath( dir & "../apps/updateserver/services" )
};
variables.artifacts = dir & "/artifacts";
if ( !DirectoryExists( variables.artifacts ))
directoryCreate( variables.artifacts );

variables.buildDir = getTempDirectory() & "/build-test-#createUniqueId()#/";
if ( DirectoryExists( variables.buildDir ) )
directoryDelete( variables.buildDir, true );
directoryCreate( variables.buildDir, true );

}

function run( testResults , testBox ) {
describe( "extensions need to be served directly for older lucee versions", function() {
it(title="check local extension cache works", body=function(){
var extensionCache = new services.extensionCache();
var path = extensionCache.getExtensionLex( "https://ext.lucee.org/compress-extension-1.0.0.15.lex" );
systemOutput( path, true )
expect( fileExists( path ) ).toBeTrue();
});

it(title="check local extension cache works (from cache)", body=function(){
var extensionCache = new services.extensionCache();
var path = extensionCache.getExtensionLex( "https://ext.lucee.org/compress-extension-1.0.0.15.lex" );
// systemOutput( path, true )
expect( fileExists( path ) ).toBeTrue();
});

});
}

}

0 comments on commit da635cb

Please sign in to comment.