Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Castopod
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Guy Martin (Dwev)
Castopod
Commits
11aa3586
Commit
11aa3586
authored
1 year ago
by
Yassine Doghri
Browse files
Options
Downloads
Patches
Plain Diff
fix(s3): add a flag to serve media files by redirecting to a presigned url instead of default proxy
parent
754e7a6b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
modules/Media/Config/Media.php
+1
-0
1 addition, 0 deletions
modules/Media/Config/Media.php
modules/Media/FileManagers/S3.php
+55
-4
55 additions, 4 deletions
modules/Media/FileManagers/S3.php
with
56 additions
and
4 deletions
modules/Media/Config/Media.php
+
1
−
0
View file @
11aa3586
...
...
@@ -33,6 +33,7 @@ class Media extends BaseConfig
'debug'
=>
false
,
'pathStyleEndpoint'
=>
false
,
'keyPrefix'
=>
''
,
'serveWithRedirect'
=>
false
,
];
/**
...
...
This diff is collapsed.
Click to expand it.
modules/Media/FileManagers/S3.php
+
55
−
4
View file @
11aa3586
...
...
@@ -10,6 +10,7 @@ use CodeIgniter\Exceptions\PageNotFoundException;
use
CodeIgniter\Files\File
;
use
CodeIgniter\HTTP\IncomingRequest
;
use
CodeIgniter\HTTP\Response
;
use
DateTime
;
use
Exception
;
use
Modules\Media\Config\Media
as
MediaConfig
;
...
...
@@ -183,8 +184,17 @@ class S3 implements FileManagerInterface
public
function
serve
(
string
$key
):
Response
{
/** @var IncomingRequest $request */
$request
=
service
(
'request'
);
if
(
$this
->
config
->
s3
[
'serveWithRedirect'
])
{
return
$this
->
servePresignedRequest
(
$key
);
}
return
$this
->
serveProxy
(
$key
);
}
private
function
serveProxy
(
string
$key
):
Response
{
/** @var IncomingRequest $incomingRequest */
$incomingRequest
=
service
(
'request'
);
/** @var Response $response */
$response
=
service
(
'response'
);
...
...
@@ -194,8 +204,8 @@ class S3 implements FileManagerInterface
'Key'
=>
$this
->
prefixKey
(
$key
),
];
if
(
$r
equest
->
h
asH
eader
(
'Range'
)
)
{
$s3Request
[
'Range'
]
=
$request
->
header
(
'Range'
)
->
getValue
();
foreach
(
$incomingR
equest
->
header
s
()
as
$header
)
{
$s3Request
[
$header
->
getName
()]
=
$header
->
getValue
();
}
try
{
...
...
@@ -217,6 +227,47 @@ class S3 implements FileManagerInterface
return
$response
->
setBody
((
string
)
$result
->
get
(
'Body'
)
->
getContents
());
}
private
function
servePresignedRequest
(
string
$key
):
Response
{
$cacheName
=
'object_presigned_uri_'
.
str_replace
(
'/'
,
'-'
,
$key
)
.
'_'
.
$this
->
config
->
s3
[
'bucket'
];
if
(
!
$found
=
cache
(
$cacheName
))
{
$cmd
=
$this
->
s3
->
getCommand
(
'GetObject'
,
[
'Bucket'
=>
$this
->
config
->
s3
[
'bucket'
],
'Key'
=>
$this
->
prefixKey
(
$key
),
]);
$request
=
$this
->
s3
->
createPresignedRequest
(
$cmd
,
'+1 day'
);
$found
=
(
string
)
$request
->
getUri
();
cache
()
->
save
(
$cacheName
,
$found
,
DAY
);
}
$cacheOptions
=
[
'max-age'
=>
DAY
,
'etag'
=>
md5
(
$found
),
'public'
=>
true
,
];
if
(
cache
()
->
getMetaData
(
$cacheName
))
{
$lastModifiedTimestamp
=
cache
()
->
getMetaData
(
$cacheName
)[
'mtime'
];
$lastModified
=
new
DateTime
();
$lastModified
->
setTimestamp
(
$lastModifiedTimestamp
);
$cacheOptions
[
'last-modified'
]
=
$lastModified
->
format
(
DATE_RFC7231
);
}
/** @var Response $response */
$response
=
service
(
'response'
);
// Remove Cache-Control header before redefining it
header_remove
(
'Cache-Control'
);
return
$response
->
setCache
(
$cacheOptions
)
->
redirect
(
$found
);
}
private
function
prefixKey
(
string
$key
):
string
{
if
(
$this
->
config
->
s3
[
'keyPrefix'
]
===
''
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment