-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support exporting source media to google drive #115
base: main
Are you sure you want to change the base?
Conversation
d6baa92
to
0ef1107
Compare
…ow for downloading/uploading large files
…nning after returning a response
6a5806f
to
f2e538f
Compare
); | ||
} | ||
|
||
offset += chunkSize; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if lambda times out during this process can we find a way of telling the user?
return folderId; | ||
}; | ||
|
||
export const uploadFileToGoogleDrive = async ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete - moved to media-export lambda
import Drive = drive_v3.Drive; | ||
import Docs = docs_v1.Docs; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure I've come across this syntax before!
|
||
if (response.ok) { | ||
// Response status is 308 until the final chunk. Final response includes file metadata | ||
return ((await response.json()) as { id: string }).id; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be zodified?
const dynamoClient = getDynamoClient( | ||
config.aws.region, | ||
config.aws.localstackEndpoint, | ||
const id = req.query.id as string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use zod to parse the request like we're doing for some of the endpoints above to avoid a type error here if query.id isn't a string?
throw new Error(`Failed to retrieve object ${key} from bucket ${bucket}`); | ||
} | ||
await downloadS3Data( | ||
data.Body as Readable, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be parsed as well?
if (isS3Failure(transcriptText)) { | ||
if (transcriptText.failureReason === 'NoSuchKey') { | ||
const msg = `Failed to export transcript - file has expired. Please re-upload the file and try again.`; | ||
logger.error(msg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be better to have separate messages for the user and for devs looking through error logs wherever we're instructing a user on what to next as we are here.
exportType: format, | ||
}; | ||
} | ||
const exportResult = await uploadToGoogleDocs( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is exportResult
the google doc's id? Might be worth renaming the variable to make this more clear.
}; | ||
|
||
export const updateStatus = ( | ||
status: ExportStatus, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe renaming to something like statusToUpdate
could make it clearer what's going on here
let currentStatuses: ExportStatuses = exportStatusInProgress( | ||
exportRequest.data.items, | ||
); | ||
await writeTranscriptionItem(dynamoClient, config.app.tableName, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it worth doing a single write to the db after the attempted uploads of text and srt files?
might also be able to simplify the code below:
let currentStatuses: ExportStatuses = await Promise.all(
exportStatusInProgress(exportRequest.data.items).map(
(exportStatus: ExportStatus) => {
if (exportStatus.exportType == 'source-media') {
return exportStatus;
}
return exportTranscriptToDoc(
config,
s3Client,
item,
exportStatus.exportType,
exportRequest.data.folderId,
driveClients.drive,
driveClients.docs,
);
},
),
);
|
||
const fileName = item.originalFilename.endsWith(`.${extensionOrMp4}`) | ||
? item.originalFilename | ||
: `${item.originalFilename}.${extensionOrMp4 || 'mp4'}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
already defaulting
: `${item.originalFilename}.${extensionOrMp4 || 'mp4'}`; | |
: `${item.originalFilename}.${extensionOrMp4}`; |
})); | ||
}; | ||
|
||
export const updateStatus = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be clearer if this was updateStatuses
plural?
What does this change?
This PR adds a new option to the export page to export the original source media to google drive. As part of this I've redesigned the export page - it now looks like this:
The export has several stages - see below for screenshots of that
On the assumption that, following this change, users will frequently be exporting more than one file, all exported files (even if there's just one) are now stored in a subfolder of the 'Guardian Transcribe Tool' folder (suffixed with date/time in case they have multiple files with the same name).
I decided to use a lambda to perform the export to google drive. This has the advantages of being easier to setup than an ECS task, and faster to start. It has the disadvantage that we can only export files up to 10GB (the maximum ephemeral storage), and we only have 15 minutes to do the upload. In my (limited) testing, I found that the lambda was able to export a 1.2GB file in 70 seconds, so I suspect we'll be limited more by the max file size than the timeout - but only just.
I had to use a separate lambda function for this rather than the API itself because API gateway has a 30s timeout, and once the lambda returns a http response it gets terminated. There are workarounds to this but I couldn't find anything that works nicely with serverless-express so I decided to create a separate function (this has the advantage that we don't need our API lambda to have loads of memory/disk space).
Some error reporting exists for if the file is too large. I still need to add an error for if the lambda times out whilst performing the export - might leave for a future PR though.
The feature relies on the file extension to tell google drive what type the file is - this seems to work reasonably well. A future feature could run apache tika or something similar on the file to determine the file type.
In theory the
uploadFileToGoogleDrive
function should be streaming the file 128MB at a time, in practice I found that the function ran out of memory when uploading a 1.2Gb file when the lambda only had 512MB. This needs more investigation - for now I have set the memory to 2GB. I think it's worth getting in as is because my 1.2GB test file was off a 1h30 youtube video, and I suspect many videos will be under this length. Might be a bit of fun though to try and work out how memory management in node works.How to test
This is currently live on CODE, you can try it out here https://transcribe.code.dev-gutools.co.uk/
Screenshots