Skip to content

Commit c4ef125

Browse files
GitHub binary fixes (#4877)
* Deal with binary files * Add uploadId * Properly load source on commits to update * deal with adding binary * fix typing * try with utf-8 * Try binary string * try something weird * add comment * deal with binary shas * properly compare binary shas * fix sha check * use sha when adding module using latest import utils * change version * explicitly check if sha is available * deal with updating private binary file * optimistically update binary change * typing
1 parent 0c9ff7e commit c4ef125

File tree

8 files changed

+227
-118
lines changed

8 files changed

+227
-118
lines changed

packages/app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
"circular-json": "^0.4.0",
108108
"codemirror": "^5.27.4",
109109
"codesandbox-api": "0.0.24",
110-
"codesandbox-import-utils": "^2.1.14",
110+
"codesandbox-import-utils": "^2.2.1",
111111
"color": "^0.11.4",
112112
"compare-versions": "^3.1.0",
113113
"console": "^0.7.2",

packages/app/src/app/overmind/effects/api/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,24 @@ export default {
139139
)
140140
.then(transformModule);
141141
},
142+
saveModulePrivateUpload(
143+
sandboxId: string,
144+
moduleShortid: string,
145+
data: {
146+
code: string;
147+
uploadId: string;
148+
sha: string;
149+
}
150+
): Promise<Module> {
151+
return api
152+
.put<IModuleAPIResponse>(
153+
`/sandboxes/${sandboxId}/modules/${moduleShortid}`,
154+
{
155+
module: data,
156+
}
157+
)
158+
.then(transformModule);
159+
},
142160
saveModules(sandboxId: string, modules: Module[]): Promise<Module[]> {
143161
return api
144162
.put<IModuleAPIResponse[]>(`/sandboxes/${sandboxId}/modules/mupdate`, {

packages/app/src/app/overmind/effects/http.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,24 @@ export default {
77
delete: axios.delete,
88
put: axios.put,
99
request: axios.request,
10+
blobToBase64: (url: string): Promise<string> =>
11+
fetch(url)
12+
.then((response) => response.blob())
13+
.then(
14+
(blob) =>
15+
new Promise((resolve, reject) => {
16+
const reader = new FileReader();
17+
reader.onload = function () {
18+
// Github interprets base64 differently, so this fixes it, insane right?
19+
// https://stackoverflow.com/questions/39234218/github-api-upload-an-image-to-repo-from-base64-array?rq=1
20+
resolve(
21+
window.btoa(
22+
window.atob((reader.result as string).replace(/^(.+,)/, ''))
23+
)
24+
);
25+
};
26+
reader.onerror = reject;
27+
reader.readAsDataURL(blob);
28+
})
29+
),
1030
};

packages/app/src/app/overmind/namespaces/files/actions.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
getDirectoryPath,
33
getModulePath,
4-
getModulesAndDirectoriesInDirectory,
4+
getModulesAndDirectoriesInDirectory
55
} from '@codesandbox/common/lib/sandbox/modules';
66
import getDefinition from '@codesandbox/common/lib/templates';
77
import { Directory, Module, UploadFile } from '@codesandbox/common/lib/types';
@@ -15,7 +15,7 @@ import denormalize from 'codesandbox-import-utils/lib/utils/files/denormalize';
1515

1616
import {
1717
resolveDirectoryWrapped,
18-
resolveModuleWrapped,
18+
resolveModuleWrapped
1919
} from '../../utils/resolve-module-wrapped';
2020
import * as internalActions from './internalActions';
2121

@@ -33,13 +33,13 @@ export const applyRecover: Action<Array<{
3333
recoveredList.forEach(({ recoverData, module }) => {
3434
actions.editor.codeChanged({
3535
moduleShortid: module.shortid,
36-
code: recoverData.code,
36+
code: recoverData.code
3737
});
3838
effects.vscode.setModuleCode(module);
3939
});
4040

4141
effects.analytics.track('Files Recovered', {
42-
fileCount: recoveredList.length,
42+
fileCount: recoveredList.length
4343
});
4444
};
4545

@@ -56,13 +56,13 @@ export const createRecoverDiffs: Action<Array<{
5656
const oldCode = module.code;
5757
actions.editor.codeChanged({
5858
moduleShortid: module.shortid,
59-
code: recoverData.code,
59+
code: recoverData.code
6060
});
6161
effects.vscode.openDiff(sandbox.id, module, oldCode);
6262
});
6363

6464
effects.analytics.track('Files Recovered', {
65-
fileCount: recoveredList.length,
65+
fileCount: recoveredList.length
6666
});
6767
};
6868

@@ -156,7 +156,7 @@ export const directoryCreated: AsyncAction<{
156156
insertedAt: new Date().toString(),
157157
updatedAt: new Date().toString(),
158158
type: 'directory' as 'directory',
159-
path: (null as unknown) as string,
159+
path: (null as unknown) as string
160160
};
161161

162162
sandbox.directories.push(optimisticDirectory as Directory);
@@ -189,7 +189,7 @@ export const directoryCreated: AsyncAction<{
189189

190190
Object.assign(directory, {
191191
id: newDirectory.id,
192-
shortid: newDirectory.shortid,
192+
shortid: newDirectory.shortid
193193
});
194194

195195
effects.live.sendDirectoryCreated(directory);
@@ -203,7 +203,7 @@ export const directoryCreated: AsyncAction<{
203203
state.editor.modulesByPath = effects.vscode.sandboxFsSync.create(sandbox);
204204
actions.internal.handleError({
205205
message: 'Unable to save new directory',
206-
error,
206+
error
207207
});
208208
}
209209
},
@@ -259,7 +259,7 @@ export const moduleMovedToDirectory: AsyncAction<{
259259
state.editor.modulesByPath = effects.vscode.sandboxFsSync.create(sandbox);
260260
actions.internal.handleError({
261261
message: 'Could not save new module location',
262-
error,
262+
error
263263
});
264264
}
265265

@@ -315,7 +315,7 @@ export const directoryMovedToDirectory: AsyncAction<{
315315
state.editor.modulesByPath = effects.vscode.sandboxFsSync.create(sandbox);
316316
actions.internal.handleError({
317317
message: 'Could not save new directory location',
318-
error,
318+
error
319319
});
320320
}
321321

@@ -349,7 +349,7 @@ export const directoryDeleted: AsyncAction<{
349349
)[0];
350350
const {
351351
removedModules,
352-
removedDirectories,
352+
removedDirectories
353353
} = getModulesAndDirectoriesInDirectory(
354354
removedDirectory,
355355
sandbox.modules,
@@ -393,7 +393,7 @@ export const directoryDeleted: AsyncAction<{
393393
state.editor.modulesByPath = effects.vscode.sandboxFsSync.create(sandbox);
394394
actions.internal.handleError({
395395
message: 'Could not delete directory',
396-
error,
396+
error
397397
});
398398
}
399399

@@ -427,7 +427,7 @@ export const directoryRenamed: AsyncAction<{
427427
actions.files.internal.renameDirectoryInState({
428428
directory,
429429
sandbox,
430-
title,
430+
title
431431
});
432432

433433
actions.editor.internal.updatePreviewCode();
@@ -443,11 +443,11 @@ export const directoryRenamed: AsyncAction<{
443443
actions.files.internal.renameDirectoryInState({
444444
directory,
445445
sandbox,
446-
title: oldTitle,
446+
title: oldTitle
447447
});
448448
actions.internal.handleError({
449449
message: 'Could not rename directory',
450-
error,
450+
error
451451
});
452452
}
453453
},
@@ -473,7 +473,7 @@ export const gotUploadedFiles: AsyncAction<string> = async (
473473
} catch (error) {
474474
actions.internal.handleError({
475475
message: 'Unable to get uploaded files information',
476-
error,
476+
error
477477
});
478478
}
479479
};
@@ -491,7 +491,7 @@ export const addedFileToSandbox: AsyncAction<Pick<
491491
title: name,
492492
directoryShortid: null,
493493
code: url,
494-
isBinary: true,
494+
isBinary: true
495495
});
496496

497497
effects.executor.updateFiles(state.editor.currentSandbox);
@@ -520,7 +520,7 @@ export const deletedUploadedFile: AsyncAction<string> = async (
520520
state.uploadedFiles.splice(index, 0, ...removedFiles);
521521
actions.internal.handleError({
522522
message: 'Unable to delete uploaded file',
523-
error,
523+
error
524524
});
525525
}
526526
};
@@ -544,28 +544,29 @@ export const filesUploaded: AsyncAction<{
544544
const { modules, directories } = await actions.files.internal.uploadFiles(
545545
{
546546
files,
547-
directoryShortid,
547+
directoryShortid
548548
}
549549
);
550550

551551
actions.files.massCreateModules({
552552
modules,
553553
directories,
554-
directoryShortid,
554+
directoryShortid
555555
});
556556

557557
effects.executor.updateFiles(sandbox);
558+
actions.git.updateGitChanges();
558559
} catch (error) {
559560
if (error.message.indexOf('413') !== -1) {
560561
actions.internal.handleError({
561562
message: `The uploaded file is bigger than 7MB, contact [email protected] if you want to raise this limit`,
562563
error,
563-
hideErrorMessage: true,
564+
hideErrorMessage: true
564565
});
565566
} else {
566567
actions.internal.handleError({
567568
message: 'Unable to upload files',
568-
error,
569+
error
569570
});
570571
}
571572
}
@@ -631,7 +632,7 @@ export const massCreateModules: AsyncAction<{
631632

632633
actions.internal.handleError({
633634
message: 'Unable to create new files',
634-
error,
635+
error
635636
});
636637
}
637638
},
@@ -662,7 +663,7 @@ export const moduleCreated: AsyncAction<{
662663
sourceId: sandbox.sourceId,
663664
isNotSynced: true,
664665
...(code ? { code } : {}),
665-
...(typeof isBinary === 'boolean' ? { isBinary } : {}),
666+
...(typeof isBinary === 'boolean' ? { isBinary } : {})
666667
});
667668

668669
// We have to push the module to the array before we can figure out its path,
@@ -733,7 +734,7 @@ export const moduleCreated: AsyncAction<{
733734

734735
actions.internal.handleError({
735736
message: 'Unable to save new file',
736-
error,
737+
error
737738
});
738739
}
739740

@@ -806,7 +807,7 @@ export const createModulesByPath: AsyncAction<{
806807
modules,
807808
directories,
808809
directoryShortid: null,
809-
cbID,
810+
cbID
810811
});
811812

812813
effects.executor.updateFiles(sandbox);
@@ -880,7 +881,7 @@ export const syncSandbox: AsyncAction<any[]> = async (
880881
actions.internal.handleError({
881882
message:
882883
"We weren't able to retrieve the latest files of the sandbox, please refresh",
883-
error,
884+
error
884885
});
885886
}
886887

0 commit comments

Comments
 (0)