Content Disposition Setting #123
Replies: 6 comments
-
Headers are set per file when uploading. This could be solved by adding an event for adding metadata when uploading, but that would still require you to have a custom module/plugin. Do you need it for all the files uploaded? If no, how would you indicate that some files need to be opened in the browser and others need to be downloaded? |
Beta Was this translation helpful? Give feedback.
-
Actually, the event idea is perfect. Thank you. Though if were to be
implemented, it's a good question. I guess I could see it being done on
that field level when you're setting up a section. Maybe a content
disposition dropdown for an Assets field (where you restrict asset types,
set selection limits and so forth) that modifies all uploads pushed via
that field in the CMS? Not sure how all that works under the hood, if
that's feasible.
Regardless, thanks again for the tip!
…On Fri, May 22, 2020 at 2:51 AM Andris Sevcenko ***@***.***> wrote:
Headers are set per file when uploading. This could be solved by adding an
event for adding metadata when uploading, but that would still require you
to have a custom module/plugin.
Do you need it for all the files uploaded? If no, how would you indicate
that some files need to be opened in the browser and others need to be
downloaded?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#85 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/APQCZ6VCKFJWZU54JX3V5G3RSYOIVANCNFSM4NGOLYSA>
.
--
Christopher Spence
Chief Technology Officer
<http://t.sidekickopen69.com/e1t/c/5/f18dQhb0S7lC8dDMPbW2n0x6l2B9nMJW7t5XYg7fK3rjW7gbCc44WYq5jW1p7ZwM56dwFHf5P0xGT02?t=https%3A%2F%2Fclick.api.drift.com%2Fclick%2F5beb1970-d578-4f01-b437-3c66f41816a5%3Fu%3Dhttp%253A%252F%252Ft.sidekickopen69.com%252Fe1t%252Fc%252F5%252Ff18dQhb0S7lC8dDMPbW2n0x6l2B9nMJW7t5XYg7fK3rjW7gbCc44WYq5jW1p7ZwM56dwFHf5P0xGT02%253Ft%253Dhttp%25253A%25252F%25252Fwww.teachupbeat.com%25252F%2526si%253D6312833021247488%2526pi%253Db0ffe455-80ba-4210-80be-27d913fb07b4%26h%3D4505504d6426d65ea7e184764ebf725d&si=6312833021247488&pi=d7a40a5c-1956-4ada-e722-1cfb03778d37>
301-518-4918 <(301)%20518-4918>
EdWeek - San Marcos CISD and Upbeat, a Partnership to Strengthen Teacher
Retention
<http://t.sidekickopen69.com/e1t/c/5/f18dQhb0S7lC8dDMPbW2n0x6l2B9nMJW7t5XYg7fK3rjW7gbCc44WYq5jW1p7ZwM56dwFHf5P0xGT02?t=https%3A%2F%2Fclick.api.drift.com%2Fclick%2F9de6b2c3-b5ba-459e-a024-42e76049682b%3Fu%3Dhttp%253A%252F%252Fblogs.edweek.org%252Fteachers%252Fteaching_now%252F2018%252F02%252Fcan_data_help_school_districts_improve_teacher_retention.html%26h%3D364f2afa35e0285d282b4ea05b1e2a11&si=6312833021247488&pi=d7a40a5c-1956-4ada-e722-1cfb03778d37>
|
Beta Was this translation helpful? Give feedback.
-
What sort of files are you wanting to have |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I'm assuming this enhancement hasn't been done. But is there any way to do it from a module/plugin? I already have a custom module for saving new assets (to an S3 volume), but I'm not sure how to set the headers (content-disposition). I need to do this for a feature on a site where a user should be able to click a |
Beta Was this translation helpful? Give feedback.
-
For anybody also struggling with this we've cooked up a solution based on @andris-sevcenko's suggestion. This solution will not work in every case, but if you wish to offer downloads on your website coming from a dedicated craft assets volume this will probably do the trick.
use Craft;
use Aws\S3\S3Client;
/* ... */
protected $_client;
protected function s3client() {
if (!$this->_client) {
$config = [
'region' => Craft::parseEnv('$S3_REGION'),
'version' => 'latest',
'credentials' => [
'key' => Craft::parseEnv('$S3_KEY_ID'),
'secret' => Craft::parseEnv('$S3_SECRET'),
],
];
$this->_client = new S3Client($config);
}
return $this->_client;
}
use craft\elements\Asset;
use craft\events\AssetEvent;
use craft\events\ElementEvent;
use craft\services\Assets;
use craft\services\Elements;
use yii\base\Event;
/* ... */
Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $e) {
// Get the element that's being saved.
$element = $e->element;
// Check if it is an asset.
if ($element instanceof Asset) {
/** @var \craft\awss3\Volume $volume */
$volume = $element->getVolume();
// Check if the volume handle matches the one you created before.
// You could also put this in environment variables or config for configurability.
if ($volume->handle === 'downloads') {
// Get the bucket and folder the item was uploaded to.
$bucket = getenv('S3_BUCKET');
$folder = $volume->subfolder.$element->folderPath;
$key = $folder.'/'.$element->filename;
// Copy the object with whatever metadata you wish to apply to it.
$this->s3client()->copyObject([
'Bucket' => $bucket,
'CopySource' => "$bucket/$key",
'Key' => $key,
'ContentDisposition' => 'attachment', // ⬅️ extra metadata
'MetadataDirective' => 'REPLACE', // ⬅️❗️ must replace because it already exists
]);
}
}
}); |
Beta Was this translation helpful? Give feedback.
-
Is there anyway to set the content-disposition header for puts to S3? I'm finding that the <a download attribute is not honored unless I manually update the metadata for objects in the S3 bucket on AWS. Thx
Beta Was this translation helpful? Give feedback.
All reactions