-
Notifications
You must be signed in to change notification settings - Fork 0
/
cms-replace-file.js
61 lines (53 loc) · 1.81 KB
/
cms-replace-file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Run in the CMS folder view to replace HTML reports
async function writeFile (fileId, contents, message) {
const submittedFormIdentifier = (
await fetch(`/entity/edit.act?id=${fileId}&type=file`).then(r => r.text())
).match(
/<input type="hidden" name="submittedFormIdentifier" value="([0-9a-z]+)"\/>/
)[1]
let formData = new FormData()
formData.set('submittedFormIdentifier', submittedFormIdentifier)
formData.set('id', fileId)
formData.set('bytesAsString', contents)
formData.set('shouldBeIndexed', 'on')
formData.set('shouldBePublished', 'on')
formData.set('customLinkRewriting', 'ABSOLUTE')
await fetch('/entity/submitfile.act?saveAs=draft', {
body: formData,
method: 'POST'
})
const submitId = (
await fetch(`/entity/open.act?type=file&id=${fileId}`).then(r => r.text())
).match(/<input type="hidden" name="id" value="([0-9a-z]+)"\/>/)[1]
formData = new FormData()
formData.set('type', 'file')
formData.set('id', submitId)
formData.set('dataCheckSettings.comments', message)
await fetch('/entity/submitDraft.act?submitButtonType=submit', {
method: 'POST',
body: formData
})
}
for (const assetRow of document.querySelectorAll(
'#DataTables_Table_0_wrapper td:nth-child(2)'
)) {
const assetId = new URL(assetRow.children[0].href).searchParams.get('id')
const label = document.createElement('label')
const fileInput = Object.assign(document.createElement('input'), {
type: 'file'
})
label.append('Replace file: ', fileInput)
assetRow.append(label)
fileInput.addEventListener('change', async () => {
if (fileInput.files[0]) {
fileInput.disabled = true
await writeFile(
assetId,
await fileInput.files[0].text(),
prompt('Change message')
)
fileInput.value = ''
fileInput.disabled = false
}
})
}