Last active
August 10, 2021 12:18
-
-
Save jweisman/3c84bd5a03ce435b443711e81316e9af to your computer and use it in GitHub Desktop.
Ingest Form for Alma Digital
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function alma_get($url) { | |
$opts = [ | |
"http" => [ | |
"header" => "Accept: application/json\r\n" . | |
"Authorization: apikey " . getenv('ALMA_APIKEY') . "\r\n" | |
] | |
]; | |
$context = stream_context_create($opts); | |
$json = file_get_contents($url, false, $context); | |
return json_decode($json); | |
} | |
$config = alma_get('https://api-na.hosted.exlibrisgroup.com/almaws/v1/conf/general?expand=ingest_form'); | |
?> | |
<h1>Alma Digital Ingest</h1> | |
<p>Select your file:</p> | |
<form method="POST" enctype="multipart/form-data" action="<?= $config->digital->ingest_url ?>"> | |
<?php | |
foreach($config->digital->ingest_form as $key => $value) { | |
print "<input type='hidden' name='".$key."' value='".$value."'>"; | |
} | |
?> | |
<input type="hidden" id="Content-Type" name="Content-Type"> | |
<input type="hidden" id="Content-MD5" name="Content-MD5"> | |
<input type=file id=file name=file> | |
<p><button >Submit</button></p> | |
</form> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script> | |
<script> | |
function calculateMD5(file) { | |
return new Promise((resolve, reject) => { | |
var reader = new FileReader(); | |
reader.onload = () => { | |
try { | |
const wordArray = CryptoJS.lib.WordArray.create(reader.result); | |
const result = CryptoJS.MD5(wordArray).toString(CryptoJS.enc.Base64); | |
resolve(result); | |
} catch (e) { | |
console.error('error', e) | |
reject(e) | |
} | |
}; | |
reader.readAsArrayBuffer(file); | |
}) | |
} | |
const form = document.forms[0]; | |
form.addEventListener('submit', async event => { | |
event.preventDefault(); | |
const file = document.getElementById('file').files[0]; | |
const md5 = await calculateMD5(file); | |
document.getElementById('Content-MD5').value = md5; | |
form.submit(); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment