fix(editorial-workflow): fix LM pointers changing to binary files (#2228)

This commit is contained in:
Benaiah Mischenko 2019-03-22 06:59:06 -07:00 committed by Shawn Erquhart
parent 96279ce155
commit d39a361e2d
2 changed files with 98 additions and 56 deletions

View File

@ -279,34 +279,33 @@ export default class GitGateway {
}, },
); );
} }
getLargeMediaDisplayURLs(mediaFiles) { async getLargeMediaDisplayURLs(mediaFiles) {
return this.getLargeMediaClient().then(client => { const client = await this.getLargeMediaClient();
const largeMediaItems = mediaFiles const largeMediaItems = mediaFiles
.filter(({ path }) => client.matchPath(path)) .filter(({ path }) => client.matchPath(path))
.map(({ id, path }) => ({ path, sha: id })); .map(({ id, path }) => ({ path, sha: id }));
return this.backend return this.backend
.fetchFiles(largeMediaItems) .fetchFiles(largeMediaItems)
.then(items => .then(items =>
items.map(({ file: { sha }, data }) => { items.map(({ file: { sha }, data }) => {
const parsedPointerFile = parsePointerFile(data); const parsedPointerFile = parsePointerFile(data);
return [ return [
{ {
pointerId: sha, pointerId: sha,
resourceId: parsedPointerFile.sha, resourceId: parsedPointerFile.sha,
}, },
parsedPointerFile, parsedPointerFile,
]; ];
}), }),
) )
.then(unzip) .then(unzip)
.then(([idMaps, files]) => .then(([idMaps, files]) =>
Promise.all([idMaps, client.getResourceDownloadURLArgs(files).then(fromPairs)]), Promise.all([idMaps, client.getResourceDownloadURLArgs(files).then(fromPairs)]),
) )
.then(([idMaps, resourceMap]) => .then(([idMaps, resourceMap]) =>
idMaps.map(({ pointerId, resourceId }) => [pointerId, resourceMap[resourceId]]), idMaps.map(({ pointerId, resourceId }) => [pointerId, resourceMap[resourceId]]),
) )
.then(fromPairs); .then(fromPairs);
});
} }
getMediaDisplayURL(displayURL) { getMediaDisplayURL(displayURL) {
@ -331,35 +330,73 @@ export default class GitGateway {
}); });
} }
persistEntry(entry, mediaFiles, options) { async getPointerFileForMediaFileObj(fileObj) {
return this.backend.persistEntry(entry, mediaFiles, options); const client = await this.getLargeMediaClient();
}
persistMedia(mediaFile, options) {
const { fileObj, path, value } = mediaFile;
const { name, size } = fileObj; const { name, size } = fileObj;
return this.getLargeMediaClient().then(client => { const sha = await getBlobSHA(fileObj);
const fixedPath = path.startsWith('/') ? path.slice(1) : path; await client.uploadResource({ sha, size }, fileObj);
if (!client.enabled || !client.matchPath(fixedPath)) { const pointerFileString = createPointerFile({ sha, size });
return this.backend.persistMedia(mediaFile, options); const pointerFileBlob = new Blob([pointerFileString]);
} const pointerFile = new File([pointerFileBlob], name, { type: 'text/plain' });
const pointerFileSHA = await getBlobSHA(pointerFile);
return {
file: pointerFile,
blob: pointerFileBlob,
sha: pointerFileSHA,
raw: pointerFileString,
};
}
return getBlobSHA(fileObj).then(async sha => { async persistEntry(entry, mediaFiles, options) {
await client.uploadResource({ sha, size }, fileObj); const client = await this.getLargeMediaClient();
const pointerFileString = createPointerFile({ sha, size }); if (!client.enabled) {
const pointerFileBlob = new Blob([pointerFileString]); return this.backend.persistEntry(entry, mediaFiles, options);
const pointerFile = new File([pointerFileBlob], name, { type: 'text/plain' }); }
const pointerFileSHA = await getBlobSHA(pointerFile);
const persistMediaArgument = { const largeMediaFilteredMediaFiles = await Promise.all(
fileObj: pointerFile, mediaFiles.map(async mediaFile => {
size: pointerFileBlob.size, const { fileObj, path } = mediaFile;
path, const fixedPath = path.startsWith('/') ? path.slice(1) : path;
sha: pointerFileSHA, if (!client.matchPath(fixedPath)) {
raw: pointerFileString, return mediaFile;
value, }
const pointerFileDetails = await this.getPointerFileForMediaFileObj(fileObj);
return {
...mediaFile,
fileObj: pointerFileDetails.file,
size: pointerFileDetails.blob.size,
sha: pointerFileDetails.sha,
raw: pointerFileDetails.raw,
}; };
return this.backend.persistMedia(persistMediaArgument, options); }),
}); );
});
return this.backend.persistEntry(entry, largeMediaFilteredMediaFiles, options);
}
async persistMedia(mediaFile, options) {
const { fileObj, path, value } = mediaFile;
const displayURL = URL.createObjectURL(fileObj);
const client = await this.getLargeMediaClient();
const fixedPath = path.startsWith('/') ? path.slice(1) : path;
if (!client.enabled || !client.matchPath(fixedPath)) {
return this.backend.persistMedia(mediaFile, options);
}
const pointerFileDetails = await this.getPointerFileForMediaFileObj(fileObj);
const persistMediaArgument = {
fileObj: pointerFileDetails.file,
size: pointerFileDetails.blob.size,
path,
sha: pointerFileDetails.sha,
raw: pointerFileDetails.raw,
value,
};
return {
...(await this.backend.persistMedia(persistMediaArgument, options)),
displayURL,
};
} }
deleteFile(path, commitMessage, options) { deleteFile(path, commitMessage, options) {
return this.backend.deleteFile(path, commitMessage, options); return this.backend.deleteFile(path, commitMessage, options);

View File

@ -159,14 +159,19 @@ export function persistMedia(file, opts = {}) {
try { try {
const id = await getBlobSHA(file); const id = await getBlobSHA(file);
const displayURL = URL.createObjectURL(file);
const assetProxy = await createAssetProxy(fileName, file, false, privateUpload); const assetProxy = await createAssetProxy(fileName, file, false, privateUpload);
dispatch(addAsset(assetProxy)); dispatch(addAsset(assetProxy));
if (!integration) { if (!integration) {
const asset = await backend.persistMedia(state.config, assetProxy); const asset = await backend.persistMedia(state.config, assetProxy);
const displayURL = asset.displayURL || URL.createObjectURL(file);
return dispatch(mediaPersisted({ id, displayURL, ...asset })); return dispatch(mediaPersisted({ id, displayURL, ...asset }));
} }
return dispatch(mediaPersisted({ id, displayURL, ...assetProxy.asset }, { privateUpload })); return dispatch(
mediaPersisted(
{ id, displayURL: URL.createObjectURL(file), ...assetProxy.asset },
{ privateUpload },
),
);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
dispatch( dispatch(