Skip to content

Commit b7a1635

Browse files
committed
add logic to decode special media types
1 parent 123bfda commit b7a1635

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

jupyter_drives/manager.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import tornado
88
import httpx
99
import traitlets
10+
import base64
1011
from jupyter_server.utils import url_path_join
1112

1213
import obstore as obs
@@ -155,7 +156,7 @@ async def unmount_drive(self, drive_name: str):
155156

156157
return
157158

158-
async def get_contents(self, drive_name, path):
159+
async def get_contents(self, drive_name, path, special_type=False):
159160
"""Get contents of a file or directory.
160161
161162
Args:
@@ -191,9 +192,18 @@ async def get_contents(self, drive_name, path):
191192

192193
# retrieve metadata of object
193194
metadata = await obs.head_async(self._content_managers[drive_name], path)
195+
196+
# for certain media type files, extracted contents need to be read as a byte array and decoded to base64 to be viewable in JupyterLab
197+
# the following extesnions correspond to a base64 file format or are of type PDF
198+
ext = os.path.splitext(path)[1]
199+
if ext == '.pdf' or ext == '.svg' or ext == '.tif' or ext == '.tiff' or ext == '.jpg' or ext == '.jpeg' or ext == '.gif' or ext == '.png' or ext == '.bmp' or ext == '.webp':
200+
processed_content = base64.b64encode(content).decode("utf-8")
201+
else:
202+
processed_content = content.decode("utf-8")
203+
194204
data = {
195205
"path": path,
196-
"content": content.decode("utf-8"),
206+
"content": processed_content,
197207
"last_modified": metadata["last_modified"].isoformat(),
198208
"size": metadata["size"]
199209
}

src/contents.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ export class Drive implements Contents.IDrive {
195195
options?: Contents.IFetchOptions
196196
): Promise<Contents.IModel> {
197197
let relativePath = '';
198-
console.log('GET localpath: ', localPath);
199198
if (localPath !== '') {
200199
// extract current drive name
201200
const currentDrive = this._drivesList.filter(
@@ -261,7 +260,6 @@ export class Drive implements Contents.IDrive {
261260
type: 'directory'
262261
};
263262
}
264-
console.log('GET: ', relativePath);
265263

266264
Contents.validateContentsModel(data);
267265
return data;

0 commit comments

Comments
 (0)