I'm trying to set an encrypted file field's value programmatically. I'm using Encrypted Files module. I have successfully configured a file field, and it works for the contents created using the node add form.
In a custom code, I'm getting a file using the
HttpClient and want to attach to a media.
I could get the file, encyrpt and save it. However, if I want to open the file, it is broken.
What I'm doing is like the following:
$client = Drupal::httpClient(); // Get data from remote page and encrypt using the encryption profile $data = (string) $client->get('https://example.com/download.php?file=123') ->getBody(); $encryptionProfile = Drupal::entityTypeManager() ->getStorage('encryption_profile') ->load('my_encryption_profile'); $encryptedData = Drupal::service('encryption') ->encrypt($data, $encryptionProfile); // Create media entity and prepare destination directory $media = Drupal::entityTypeManager()->getStorage('media') ->create(['bundle' => 'my_media_type']); $destination = 'encrypt://my_encryption_profile/path/to/files' Drupal::service('file_system') ->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY); // Save data as file and attach to the media $filename = 'download.pdf'; $file = file_save_data($encryptedData, $destination . '/' . $filename); $media->set('encrypted_file', ['target_id' => $file->id()]); $media->save();