Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
format: "text"
};
} else {
// We shouldn't save directories
return empty();
}
switch (action.type) {
case actionTypes.DOWNLOAD_CONTENT: {
// FIXME: Convert this to downloadString, so it works for both files & notebooks
if (
content.type === "notebook" &&
typeof serializedData === "object"
) {
downloadString(
stringifyNotebook(serializedData),
filepath || "notebook.ipynb",
"application/json"
);
} else if (
content.type === "file" &&
typeof serializedData === "string"
) {
downloadString(
serializedData,
filepath,
content.mimetype || "application/octet-stream"
);
} else {
// This shouldn't happen, is here for safety
return empty();
}
error: new Error("No serialized model created for this content."),
contentRef: action.payload.contentRef
})
);
}
switch (action.type) {
case actions.DOWNLOAD_CONTENT: {
// FIXME: Convert this to downloadString, so it works for
// both files & notebooks
if (
content.type === "notebook" &&
typeof serializedData === "object"
) {
downloadString(
stringifyNotebook(serializedData),
filepath || "notebook.ipynb",
"application/json"
);
} else if (
content.type === "file" &&
typeof serializedData === "string"
) {
downloadString(
serializedData,
filepath,
content.mimetype || "application/octet-stream"
);
} else {
// This shouldn't happen, is here for safety
return empty();
}
it("calls FileSaver.saveAs with notebook and filename", () => {
const filename = "/here/there/awesome.ipynb";
const expectedData = bigDummyJSON;
expect(FileSaver.saveAs).not.toHaveBeenCalled();
downloadString(
stringifyNotebook(bigDummyJSON),
filename,
"application/json"
);
expect(FileSaver.saveAs).toHaveBeenCalledTimes(1);
const actualMockBlobResponse = FileSaver.saveAs.mock.calls[0][0];
const actualFilename = FileSaver.saveAs.mock.calls[0][1];
expect(actualMockBlobResponse).toEqual({
content: [stringifyNotebook(expectedData)],
options: { type: "application/json" }
});
expect(actualFilename).toBe("awesome.ipynb");
});
});
it("calls FileSaver.saveAs with notebook and filename", () => {
const filename = "/here/there/awesome.ipynb";
const expectedData = fixtureJSON;
expect(FileSaver.saveAs).not.toHaveBeenCalled();
downloadString(
stringifyNotebook(fixtureJSON),
filename,
"application/json"
);
expect(FileSaver.saveAs).toHaveBeenCalledTimes(1);
const actualMockBlobResponse = (FileSaver.saveAs as any).mock.calls[0][0];
const actualFilename = (FileSaver.saveAs as any).mock.calls[0][1];
expect(actualMockBlobResponse).toEqual({
content: [stringifyNotebook(expectedData)],
options: { type: "application/json" }
});
expect(actualFilename).toBe("awesome.ipynb");
});
});
it("calls FileSaver.saveAs with notebook and filename", () => {
const filename = "/here/there/awesome.ipynb";
const expectedData = bigDummyJSON;
expect(FileSaver.saveAs).not.toHaveBeenCalled();
downloadString(
stringifyNotebook(bigDummyJSON),
filename,
"application/json"
);
expect(FileSaver.saveAs).toHaveBeenCalledTimes(1);
const actualMockBlobResponse = FileSaver.saveAs.mock.calls[0][0];
const actualFilename = FileSaver.saveAs.mock.calls[0][1];
expect(actualMockBlobResponse).toEqual({
content: [stringifyNotebook(expectedData)],
options: { type: "application/json" }
});
expect(actualFilename).toBe("awesome.ipynb");
});
});
it("calls FileSaver.saveAs with notebook and filename", () => {
const filename = "/here/there/awesome.ipynb";
const expectedData = fixtureJSON;
expect(FileSaver.saveAs).not.toHaveBeenCalled();
downloadString(
stringifyNotebook(fixtureJSON),
filename,
"application/json"
);
expect(FileSaver.saveAs).toHaveBeenCalledTimes(1);
const actualMockBlobResponse = (FileSaver.saveAs as any).mock.calls[0][0];
const actualFilename = (FileSaver.saveAs as any).mock.calls[0][1];
expect(actualMockBlobResponse).toEqual({
content: [stringifyNotebook(expectedData)],
options: { type: "application/json" }
});
expect(actualFilename).toBe("awesome.ipynb");
});
});
export const downloadNotebook = (
immutableNotebook: ?ImmutableNotebook,
path: ?string
) => {
if (immutableNotebook) {
const notebook = commutable.toJS(immutableNotebook);
const filename = (path || DEFAULT_NOTEBOOK_FILENAME).split("/").pop();
const data = commutable.stringifyNotebook(notebook);
const blob = new Blob([data], { type: "text/json" });
FileSaver.saveAs(blob, filename);
}
};
notebookJS => {
if (notebookJS) {
return commutable.stringifyNotebook(notebookJS);
}
return "";
}
);
);
}
const model = content.model;
if (!model || model.type !== "notebook") {
return of(
actions.saveFailed({
contentRef: action.payload.contentRef,
error: new Error("no notebook loaded to save")
})
);
}
const filepath = content.filepath;
const appVersion = selectors.appVersion(state);
const notebook = stringifyNotebook(
toJS(
model.notebook.setIn(["metadata", "nteract", "version"], appVersion)
)
);
return writeFileObservable(filepath, notebook).pipe(
map(() => {
if (process.platform !== "darwin") {
const notificationSystem = selectors.notificationSystem(
state$.value
);
notificationSystem.addNotification({
autoDismiss: 2,
level: "success",
title: "Save successful!"
});
}
notebookJS => {
if (notebookJS) {
return commutable.stringifyNotebook(notebookJS);
}
return "";
}
);