Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
4
define(function(require) {
5
"use strict";
6
7
var $ = require('jquery');
8
var utils = require('base/js/utils');
9
10
var Contents = function(options) {
11
/**
12
* Constructor
13
*
14
* A contents handles passing file operations
15
* to the back-end. This includes checkpointing
16
* with the normal file operations.
17
*
18
* Parameters:
19
* options: dictionary
20
* Dictionary of keyword arguments.
21
* base_url: string
22
*/
23
this.base_url = options.base_url;
24
};
25
26
/** Error type */
27
Contents.DIRECTORY_NOT_EMPTY_ERROR = 'DirectoryNotEmptyError';
28
29
Contents.DirectoryNotEmptyError = function() {
30
// Constructor
31
//
32
// An error representing the result of attempting to delete a non-empty
33
// directory.
34
this.message = 'A directory must be empty before being deleted.';
35
};
36
37
Contents.DirectoryNotEmptyError.prototype = Object.create(Error.prototype);
38
Contents.DirectoryNotEmptyError.prototype.name =
39
Contents.DIRECTORY_NOT_EMPTY_ERROR;
40
41
42
Contents.prototype.api_url = function() {
43
var url_parts = [this.base_url, 'api/contents'].concat(
44
Array.prototype.slice.apply(arguments));
45
return utils.url_join_encode.apply(null, url_parts);
46
};
47
48
/**
49
* Creates a basic error handler that wraps a jqXHR error as an Error.
50
*
51
* Takes a callback that accepts an Error, and returns a callback that can
52
* be passed directly to $.ajax, which will wrap the error from jQuery
53
* as an Error, and pass that to the original callback.
54
*
55
* @method create_basic_error_handler
56
* @param{Function} callback
57
* @return{Function}
58
*/
59
Contents.prototype.create_basic_error_handler = function(callback) {
60
if (!callback) {
61
return utils.log_ajax_error;
62
}
63
return function(xhr, status, error) {
64
callback(utils.wrap_ajax_error(xhr, status, error));
65
};
66
};
67
68
/**
69
* File Functions (including notebook operations)
70
*/
71
72
/**
73
* Get a file.
74
*
75
* @method get
76
* @param {String} path
77
* @param {Object} options
78
* type : 'notebook', 'file', or 'directory'
79
* format: 'text' or 'base64'; only relevant for type: 'file'
80
* content: true or false; // whether to include the content
81
*/
82
Contents.prototype.get = function (path, options) {
83
/**
84
* We do the call with settings so we can set cache to false.
85
*/
86
var settings = {
87
processData : false,
88
cache : false,
89
type : "GET",
90
dataType : "json",
91
};
92
var url = this.api_url(path);
93
var params = {};
94
if (options.type) { params.type = options.type; }
95
if (options.format) { params.format = options.format; }
96
if (options.content === false) { params.content = '0'; }
97
return utils.promising_ajax(url + '?' + $.param(params), settings);
98
};
99
100
101
/**
102
* Creates a new untitled file or directory in the specified directory path.
103
*
104
* @method new
105
* @param {String} path: the directory in which to create the new file/directory
106
* @param {Object} options:
107
* ext: file extension to use
108
* type: model type to create ('notebook', 'file', or 'directory')
109
*/
110
Contents.prototype.new_untitled = function(path, options) {
111
var data = JSON.stringify({
112
ext: options.ext,
113
type: options.type
114
});
115
116
var settings = {
117
processData : false,
118
type : "POST",
119
data: data,
120
dataType : "json",
121
};
122
return utils.promising_ajax(this.api_url(path), settings);
123
};
124
125
Contents.prototype.delete = function(path) {
126
var settings = {
127
processData : false,
128
type : "DELETE",
129
dataType : "json",
130
};
131
var url = this.api_url(path);
132
return utils.promising_ajax(url, settings).catch(
133
// Translate certain errors to more specific ones.
134
function(error) {
135
// TODO: update IPEP27 to specify errors more precisely, so
136
// that error types can be detected here with certainty.
137
if (error.xhr.status === 400) {
138
throw new Contents.DirectoryNotEmptyError();
139
}
140
throw error;
141
}
142
);
143
};
144
145
Contents.prototype.rename = function(path, new_path) {
146
var data = {path: new_path};
147
var settings = {
148
processData : false,
149
type : "PATCH",
150
data : JSON.stringify(data),
151
dataType: "json",
152
contentType: 'application/json',
153
};
154
var url = this.api_url(path);
155
return utils.promising_ajax(url, settings);
156
};
157
158
Contents.prototype.save = function(path, model) {
159
/**
160
* We do the call with settings so we can set cache to false.
161
*/
162
var settings = {
163
processData : false,
164
type : "PUT",
165
dataType: "json",
166
data : JSON.stringify(model),
167
contentType: 'application/json',
168
};
169
var url = this.api_url(path);
170
return utils.promising_ajax(url, settings);
171
};
172
173
Contents.prototype.copy = function(from_file, to_dir) {
174
/**
175
* Copy a file into a given directory via POST
176
* The server will select the name of the copied file
177
*/
178
var url = this.api_url(to_dir);
179
180
var settings = {
181
processData : false,
182
type: "POST",
183
data: JSON.stringify({copy_from: from_file}),
184
dataType : "json",
185
};
186
return utils.promising_ajax(url, settings);
187
};
188
189
/**
190
* Checkpointing Functions
191
*/
192
193
Contents.prototype.create_checkpoint = function(path) {
194
var url = this.api_url(path, 'checkpoints');
195
var settings = {
196
type : "POST",
197
dataType : "json",
198
};
199
return utils.promising_ajax(url, settings);
200
};
201
202
Contents.prototype.list_checkpoints = function(path) {
203
var url = this.api_url(path, 'checkpoints');
204
var settings = {
205
type : "GET",
206
cache: false,
207
dataType: "json",
208
};
209
return utils.promising_ajax(url, settings);
210
};
211
212
Contents.prototype.restore_checkpoint = function(path, checkpoint_id) {
213
var url = this.api_url(path, 'checkpoints', checkpoint_id);
214
var settings = {
215
type : "POST",
216
};
217
return utils.promising_ajax(url, settings);
218
};
219
220
Contents.prototype.delete_checkpoint = function(path, checkpoint_id) {
221
var url = this.api_url(path, 'checkpoints', checkpoint_id);
222
var settings = {
223
type : "DELETE",
224
};
225
return utils.promising_ajax(url, settings);
226
};
227
228
/**
229
* File management functions
230
*/
231
232
/**
233
* List notebooks and directories at a given path
234
*
235
* On success, load_callback is called with an array of dictionaries
236
* representing individual files or directories. Each dictionary has
237
* the keys:
238
* type: "notebook" or "directory"
239
* created: created date
240
* last_modified: last modified dat
241
* @method list_notebooks
242
* @param {String} path The path to list notebooks in
243
*/
244
Contents.prototype.list_contents = function(path) {
245
return this.get(path, {type: 'directory'});
246
};
247
248
return {'Contents': Contents};
249
});
250
251