blob: 1ce59097b7110f39f13afee4f4c0a9c5e44bec54 [file] [log] [blame]
Eduardo Sousa0593aba2019-06-04 12:55:43 +01001# Copyright 2019 Canonical
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14#
15# For those usages not covered by the Apache License, Version 2.0 please
16# contact: eduardo.sousa@canonical.com
17##
aticig3dd0db62022-03-04 19:35:45 +030018import datetime
beierlmff2e8262020-07-08 16:32:50 -040019import errno
Eduardo Sousa0593aba2019-06-04 12:55:43 +010020from http import HTTPStatus
beierlmff2e8262020-07-08 16:32:50 -040021from io import BytesIO, StringIO
22import logging
Eduardo Sousa0593aba2019-06-04 12:55:43 +010023import os
bravof98fc8f02021-11-04 21:16:00 -030024import tarfile
25import zipfile
beierlmff2e8262020-07-08 16:32:50 -040026
aticig3dd0db62022-03-04 19:35:45 +030027from gridfs import errors, GridFSBucket
Eduardo Sousa0593aba2019-06-04 12:55:43 +010028from osm_common.fsbase import FsBase, FsException
beierlmff2e8262020-07-08 16:32:50 -040029from pymongo import MongoClient
30
Eduardo Sousa0593aba2019-06-04 12:55:43 +010031
32__author__ = "Eduardo Sousa <eduardo.sousa@canonical.com>"
33
34
35class GridByteStream(BytesIO):
36 def __init__(self, filename, fs, mode):
37 BytesIO.__init__(self)
38 self._id = None
39 self.filename = filename
40 self.fs = fs
41 self.mode = mode
David Garcia7982b782020-05-20 12:09:37 +020042 self.file_type = "file" # Set "file" as default file_type
Eduardo Sousa0593aba2019-06-04 12:55:43 +010043
44 self.__initialize__()
45
46 def __initialize__(self):
47 grid_file = None
48
49 cursor = self.fs.find({"filename": self.filename})
50
51 for requested_file in cursor:
52 exception_file = next(cursor, None)
53
54 if exception_file:
garciadeblas2644b762021-03-24 09:21:01 +010055 raise FsException(
56 "Multiple files found", http_code=HTTPStatus.INTERNAL_SERVER_ERROR
57 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +010058
sousaedub95cca62020-03-12 11:12:25 +000059 if requested_file.metadata["type"] in ("file", "sym"):
Eduardo Sousa0593aba2019-06-04 12:55:43 +010060 grid_file = requested_file
sousaedub95cca62020-03-12 11:12:25 +000061 self.file_type = requested_file.metadata["type"]
Eduardo Sousa0593aba2019-06-04 12:55:43 +010062 else:
garciadeblas2644b762021-03-24 09:21:01 +010063 raise FsException(
64 "Type isn't file", http_code=HTTPStatus.INTERNAL_SERVER_ERROR
65 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +010066
67 if grid_file:
68 self._id = grid_file._id
69 self.fs.download_to_stream(self._id, self)
70
71 if "r" in self.mode:
72 self.seek(0, 0)
73
74 def close(self):
75 if "r" in self.mode:
76 super(GridByteStream, self).close()
77 return
78
79 if self._id:
80 self.fs.delete(self._id)
81
garciadeblas2644b762021-03-24 09:21:01 +010082 cursor = self.fs.find(
83 {"filename": self.filename.split("/")[0], "metadata": {"type": "dir"}}
84 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +010085
86 parent_dir = next(cursor, None)
87
88 if not parent_dir:
89 parent_dir_name = self.filename.split("/")[0]
garciadeblas2644b762021-03-24 09:21:01 +010090 self.filename = self.filename.replace(
91 parent_dir_name, parent_dir_name[:-1], 1
92 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +010093
94 self.seek(0, 0)
95 if self._id:
96 self.fs.upload_from_stream_with_id(
garciadeblas2644b762021-03-24 09:21:01 +010097 self._id, self.filename, self, metadata={"type": self.file_type}
Eduardo Sousa0593aba2019-06-04 12:55:43 +010098 )
99 else:
100 self.fs.upload_from_stream(
garciadeblas2644b762021-03-24 09:21:01 +0100101 self.filename, self, metadata={"type": self.file_type}
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100102 )
103 super(GridByteStream, self).close()
104
105 def __enter__(self):
106 return self
107
108 def __exit__(self, exc_type, exc_val, exc_tb):
109 self.close()
110
111
112class GridStringStream(StringIO):
113 def __init__(self, filename, fs, mode):
114 StringIO.__init__(self)
115 self._id = None
116 self.filename = filename
117 self.fs = fs
118 self.mode = mode
David Garcia7982b782020-05-20 12:09:37 +0200119 self.file_type = "file" # Set "file" as default file_type
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100120
121 self.__initialize__()
122
123 def __initialize__(self):
124 grid_file = None
125
126 cursor = self.fs.find({"filename": self.filename})
127
128 for requested_file in cursor:
129 exception_file = next(cursor, None)
130
131 if exception_file:
garciadeblas2644b762021-03-24 09:21:01 +0100132 raise FsException(
133 "Multiple files found", http_code=HTTPStatus.INTERNAL_SERVER_ERROR
134 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100135
sousaedub95cca62020-03-12 11:12:25 +0000136 if requested_file.metadata["type"] in ("file", "dir"):
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100137 grid_file = requested_file
sousaedub95cca62020-03-12 11:12:25 +0000138 self.file_type = requested_file.metadata["type"]
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100139 else:
garciadeblas2644b762021-03-24 09:21:01 +0100140 raise FsException(
141 "File type isn't file", http_code=HTTPStatus.INTERNAL_SERVER_ERROR
142 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100143
144 if grid_file:
145 stream = BytesIO()
146 self._id = grid_file._id
147 self.fs.download_to_stream(self._id, stream)
148 stream.seek(0)
149 self.write(stream.read().decode("utf-8"))
150 stream.close()
151
152 if "r" in self.mode:
153 self.seek(0, 0)
154
155 def close(self):
156 if "r" in self.mode:
157 super(GridStringStream, self).close()
158 return
159
160 if self._id:
161 self.fs.delete(self._id)
162
garciadeblas2644b762021-03-24 09:21:01 +0100163 cursor = self.fs.find(
164 {"filename": self.filename.split("/")[0], "metadata": {"type": "dir"}}
165 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100166
167 parent_dir = next(cursor, None)
168
169 if not parent_dir:
170 parent_dir_name = self.filename.split("/")[0]
garciadeblas2644b762021-03-24 09:21:01 +0100171 self.filename = self.filename.replace(
172 parent_dir_name, parent_dir_name[:-1], 1
173 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100174
175 self.seek(0, 0)
176 stream = BytesIO()
177 stream.write(self.read().encode("utf-8"))
178 stream.seek(0, 0)
179 if self._id:
180 self.fs.upload_from_stream_with_id(
garciadeblas2644b762021-03-24 09:21:01 +0100181 self._id, self.filename, stream, metadata={"type": self.file_type}
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100182 )
183 else:
184 self.fs.upload_from_stream(
garciadeblas2644b762021-03-24 09:21:01 +0100185 self.filename, stream, metadata={"type": self.file_type}
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100186 )
187 stream.close()
188 super(GridStringStream, self).close()
189
190 def __enter__(self):
191 return self
192
193 def __exit__(self, exc_type, exc_val, exc_tb):
194 self.close()
195
196
197class FsMongo(FsBase):
garciadeblas2644b762021-03-24 09:21:01 +0100198 def __init__(self, logger_name="fs", lock=False):
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100199 super().__init__(logger_name, lock)
200 self.path = None
201 self.client = None
202 self.fs = None
203
tiernob07e4ef2020-05-06 14:22:48 +0000204 def __update_local_fs(self, from_path=None):
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100205 dir_cursor = self.fs.find({"metadata.type": "dir"}, no_cursor_timeout=True)
206
bravoff73a9002021-11-23 10:34:43 -0300207 valid_paths = []
208
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100209 for directory in dir_cursor:
tiernob07e4ef2020-05-06 14:22:48 +0000210 if from_path and not directory.filename.startswith(from_path):
211 continue
beierlm3d82ba22022-04-19 14:12:50 -0400212 self.logger.debug("Making dir {}".format(self.path + directory.filename))
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100213 os.makedirs(self.path + directory.filename, exist_ok=True)
bravoff73a9002021-11-23 10:34:43 -0300214 valid_paths.append(self.path + directory.filename)
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100215
garciadeblas2644b762021-03-24 09:21:01 +0100216 file_cursor = self.fs.find(
217 {"metadata.type": {"$in": ["file", "sym"]}}, no_cursor_timeout=True
218 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100219
220 for writing_file in file_cursor:
tiernob07e4ef2020-05-06 14:22:48 +0000221 if from_path and not writing_file.filename.startswith(from_path):
222 continue
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100223 file_path = self.path + writing_file.filename
David Garcia8ab6cc62020-06-26 17:04:37 +0200224
225 if writing_file.metadata["type"] == "sym":
226 with BytesIO() as b:
227 self.fs.download_to_stream(writing_file._id, b)
228 b.seek(0)
229 link = b.read().decode("utf-8")
beierlmff2e8262020-07-08 16:32:50 -0400230
231 try:
beierlm3d82ba22022-04-19 14:12:50 -0400232 self.logger.debug("Sync removing {}".format(file_path))
beierlmff2e8262020-07-08 16:32:50 -0400233 os.remove(file_path)
234 except OSError as e:
235 if e.errno != errno.ENOENT:
236 # This is probably permission denied or worse
237 raise
David Garcia8ab6cc62020-06-26 17:04:37 +0200238 os.symlink(link, file_path)
239 else:
bravoff73a9002021-11-23 10:34:43 -0300240 folder = os.path.dirname(file_path)
241 if folder not in valid_paths:
beierlm3d82ba22022-04-19 14:12:50 -0400242 self.logger.debug("Sync local directory {}".format(file_path))
bravoff73a9002021-11-23 10:34:43 -0300243 os.makedirs(folder, exist_ok=True)
garciadeblas2644b762021-03-24 09:21:01 +0100244 with open(file_path, "wb+") as file_stream:
beierlm3d82ba22022-04-19 14:12:50 -0400245 self.logger.debug("Sync download {}".format(file_path))
David Garcia8ab6cc62020-06-26 17:04:37 +0200246 self.fs.download_to_stream(writing_file._id, file_stream)
247 if "permissions" in writing_file.metadata:
sousaedub95cca62020-03-12 11:12:25 +0000248 os.chmod(file_path, writing_file.metadata["permissions"])
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100249
250 def get_params(self):
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100251 return {"fs": "mongo", "path": self.path}
252
253 def fs_connect(self, config):
254 try:
255 if "logger_name" in config:
256 self.logger = logging.getLogger(config["logger_name"])
257 if "path" in config:
258 self.path = config["path"]
259 else:
garciadeblas2644b762021-03-24 09:21:01 +0100260 raise FsException('Missing parameter "path"')
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100261 if not self.path.endswith("/"):
262 self.path += "/"
263 if not os.path.exists(self.path):
garciadeblas2644b762021-03-24 09:21:01 +0100264 raise FsException(
265 "Invalid configuration param at '[storage]': path '{}' does not exist".format(
266 config["path"]
267 )
268 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100269 elif not os.access(self.path, os.W_OK):
garciadeblas2644b762021-03-24 09:21:01 +0100270 raise FsException(
271 "Invalid configuration param at '[storage]': path '{}' is not writable".format(
272 config["path"]
273 )
274 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100275 if all(key in config.keys() for key in ["uri", "collection"]):
276 self.client = MongoClient(config["uri"])
277 self.fs = GridFSBucket(self.client[config["collection"]])
278 elif all(key in config.keys() for key in ["host", "port", "collection"]):
279 self.client = MongoClient(config["host"], config["port"])
280 self.fs = GridFSBucket(self.client[config["collection"]])
281 else:
282 if "collection" not in config.keys():
garciadeblas2644b762021-03-24 09:21:01 +0100283 raise FsException('Missing parameter "collection"')
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100284 else:
garciadeblas2644b762021-03-24 09:21:01 +0100285 raise FsException('Missing parameters: "uri" or "host" + "port"')
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100286 except FsException:
287 raise
288 except Exception as e: # TODO refine
289 raise FsException(str(e))
290
291 def fs_disconnect(self):
292 pass # TODO
293
294 def mkdir(self, folder):
295 """
296 Creates a folder or parent object location
297 :param folder:
298 :return: None or raises an exception
299 """
beierlm40f57c82022-04-14 14:45:07 -0400300 folder = folder.rstrip("/")
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100301 try:
garciadeblas2644b762021-03-24 09:21:01 +0100302 self.fs.upload_from_stream(folder, BytesIO(), metadata={"type": "dir"})
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100303 except errors.FileExists: # make it idempotent
304 pass
305 except Exception as e:
306 raise FsException(str(e), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
307
308 def dir_rename(self, src, dst):
309 """
310 Rename one directory name. If dst exist, it replaces (deletes) existing directory
311 :param src: source directory
312 :param dst: destination directory
313 :return: None or raises and exception
314 """
beierlm40f57c82022-04-14 14:45:07 -0400315 dst = dst.rstrip("/")
316 src = src.rstrip("/")
317
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100318 try:
319 dst_cursor = self.fs.find(
garciadeblas2644b762021-03-24 09:21:01 +0100320 {"filename": {"$regex": "^{}(/|$)".format(dst)}}, no_cursor_timeout=True
321 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100322
323 for dst_file in dst_cursor:
324 self.fs.delete(dst_file._id)
325
326 src_cursor = self.fs.find(
garciadeblas2644b762021-03-24 09:21:01 +0100327 {"filename": {"$regex": "^{}(/|$)".format(src)}}, no_cursor_timeout=True
328 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100329
330 for src_file in src_cursor:
331 self.fs.rename(src_file._id, src_file.filename.replace(src, dst, 1))
332 except Exception as e:
333 raise FsException(str(e), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
334
335 def file_exists(self, storage, mode=None):
336 """
337 Indicates if "storage" file exist
338 :param storage: can be a str or a str list
339 :param mode: can be 'file' exist as a regular file; 'dir' exists as a directory or; 'None' just exists
340 :return: True, False
341 """
342 f = storage if isinstance(storage, str) else "/".join(storage)
beierlm40f57c82022-04-14 14:45:07 -0400343 f = f.rstrip("/")
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100344
345 cursor = self.fs.find({"filename": f})
346
347 for requested_file in cursor:
348 exception_file = next(cursor, None)
349
350 if exception_file:
garciadeblas2644b762021-03-24 09:21:01 +0100351 raise FsException(
352 "Multiple files found", http_code=HTTPStatus.INTERNAL_SERVER_ERROR
353 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100354
beierlm40f57c82022-04-14 14:45:07 -0400355 self.logger.debug("Entry {} metadata {}".format(f, requested_file.metadata))
bravof98fc8f02021-11-04 21:16:00 -0300356
lloretgallegf296d2a2020-09-02 09:36:24 +0000357 # if no special mode is required just check it does exists
358 if not mode:
359 return True
360
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100361 if requested_file.metadata["type"] == mode:
362 return True
beierlmff2e8262020-07-08 16:32:50 -0400363
sousaedub95cca62020-03-12 11:12:25 +0000364 if requested_file.metadata["type"] == "sym" and mode == "file":
365 return True
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100366
367 return False
368
369 def file_size(self, storage):
370 """
371 return file size
372 :param storage: can be a str or a str list
373 :return: file size
374 """
375 f = storage if isinstance(storage, str) else "/".join(storage)
beierlm40f57c82022-04-14 14:45:07 -0400376 f = f.rstrip("/")
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100377
378 cursor = self.fs.find({"filename": f})
379
380 for requested_file in cursor:
381 exception_file = next(cursor, None)
382
383 if exception_file:
garciadeblas2644b762021-03-24 09:21:01 +0100384 raise FsException(
385 "Multiple files found", http_code=HTTPStatus.INTERNAL_SERVER_ERROR
386 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100387
388 return requested_file.length
389
bravof98fc8f02021-11-04 21:16:00 -0300390 def file_extract(self, compressed_object, path):
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100391 """
392 extract a tar file
bravof98fc8f02021-11-04 21:16:00 -0300393 :param compressed_object: object of type tar or zip
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100394 :param path: can be a str or a str list, or a tar object where to extract the tar_object
395 :return: None
396 """
397 f = path if isinstance(path, str) else "/".join(path)
beierlm40f57c82022-04-14 14:45:07 -0400398 f = f.rstrip("/")
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100399
bravof98fc8f02021-11-04 21:16:00 -0300400 if type(compressed_object) is tarfile.TarFile:
401 for member in compressed_object.getmembers():
402 if member.isfile():
403 stream = compressed_object.extractfile(member)
404 elif member.issym():
405 stream = BytesIO(member.linkname.encode("utf-8"))
406 else:
407 stream = BytesIO()
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100408
bravof98fc8f02021-11-04 21:16:00 -0300409 if member.isfile():
410 file_type = "file"
411 elif member.issym():
412 file_type = "sym"
413 else:
414 file_type = "dir"
sousaedub95cca62020-03-12 11:12:25 +0000415
bravof98fc8f02021-11-04 21:16:00 -0300416 metadata = {"type": file_type, "permissions": member.mode}
beierlm40f57c82022-04-14 14:45:07 -0400417 member.name = member.name.rstrip("/")
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100418
beierlm3d82ba22022-04-19 14:12:50 -0400419 self.logger.debug("Uploading {}/{}".format(f, member.name))
bravof98fc8f02021-11-04 21:16:00 -0300420 self.fs.upload_from_stream(
421 f + "/" + member.name, stream, metadata=metadata
422 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100423
bravof98fc8f02021-11-04 21:16:00 -0300424 stream.close()
425 elif type(compressed_object) is zipfile.ZipFile:
426 for member in compressed_object.infolist():
427 if member.is_dir():
428 stream = BytesIO()
429 else:
430 stream = compressed_object.read(member)
431
432 if member.is_dir():
433 file_type = "dir"
434 else:
435 file_type = "file"
436
437 metadata = {"type": file_type}
beierlm40f57c82022-04-14 14:45:07 -0400438 member.filename = member.filename.rstrip("/")
bravof98fc8f02021-11-04 21:16:00 -0300439
beierlm3d82ba22022-04-19 14:12:50 -0400440 self.logger.debug("Uploading {}/{}".format(f, member.filename))
bravof98fc8f02021-11-04 21:16:00 -0300441 self.fs.upload_from_stream(
442 f + "/" + member.filename, stream, metadata=metadata
443 )
444
445 if member.is_dir():
446 stream.close()
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100447
448 def file_open(self, storage, mode):
449 """
450 Open a file
451 :param storage: can be a str or list of str
452 :param mode: file mode
453 :return: file object
454 """
455 try:
456 f = storage if isinstance(storage, str) else "/".join(storage)
beierlm40f57c82022-04-14 14:45:07 -0400457 f = f.rstrip("/")
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100458
459 if "b" in mode:
460 return GridByteStream(f, self.fs, mode)
461 else:
462 return GridStringStream(f, self.fs, mode)
463 except errors.NoFile:
garciadeblas2644b762021-03-24 09:21:01 +0100464 raise FsException(
465 "File {} does not exist".format(f), http_code=HTTPStatus.NOT_FOUND
466 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100467 except IOError:
garciadeblas2644b762021-03-24 09:21:01 +0100468 raise FsException(
469 "File {} cannot be opened".format(f), http_code=HTTPStatus.BAD_REQUEST
470 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100471
472 def dir_ls(self, storage):
473 """
474 return folder content
475 :param storage: can be a str or list of str
476 :return: folder content
477 """
478 try:
479 f = storage if isinstance(storage, str) else "/".join(storage)
beierlm40f57c82022-04-14 14:45:07 -0400480 f = f.rstrip("/")
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100481
482 files = []
483 dir_cursor = self.fs.find({"filename": f})
484 for requested_dir in dir_cursor:
485 exception_dir = next(dir_cursor, None)
486
487 if exception_dir:
garciadeblas2644b762021-03-24 09:21:01 +0100488 raise FsException(
489 "Multiple directories found",
490 http_code=HTTPStatus.INTERNAL_SERVER_ERROR,
491 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100492
493 if requested_dir.metadata["type"] != "dir":
garciadeblas2644b762021-03-24 09:21:01 +0100494 raise FsException(
495 "File {} does not exist".format(f),
496 http_code=HTTPStatus.NOT_FOUND,
497 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100498
bravof98fc8f02021-11-04 21:16:00 -0300499 if f.endswith("/"):
500 f = f[:-1]
501
garciadeblas2644b762021-03-24 09:21:01 +0100502 files_cursor = self.fs.find(
503 {"filename": {"$regex": "^{}/([^/])*".format(f)}}
504 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100505 for children_file in files_cursor:
garciadeblas2644b762021-03-24 09:21:01 +0100506 files += [children_file.filename.replace(f + "/", "", 1)]
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100507
508 return files
509 except IOError:
garciadeblas2644b762021-03-24 09:21:01 +0100510 raise FsException(
511 "File {} cannot be opened".format(f), http_code=HTTPStatus.BAD_REQUEST
512 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100513
514 def file_delete(self, storage, ignore_non_exist=False):
515 """
516 Delete storage content recursively
517 :param storage: can be a str or list of str
518 :param ignore_non_exist: not raise exception if storage does not exist
519 :return: None
520 """
521 try:
522 f = storage if isinstance(storage, str) else "/".join(storage)
beierlm40f57c82022-04-14 14:45:07 -0400523 f = f.rstrip("/")
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100524
525 file_cursor = self.fs.find({"filename": f})
526 found = False
527 for requested_file in file_cursor:
528 found = True
529 exception_file = next(file_cursor, None)
530
531 if exception_file:
beierlm3d82ba22022-04-19 14:12:50 -0400532 self.logger.error(
533 "Cannot delete duplicate file: {} and {}".format(
534 requested_file.filename, exception_file.filename
535 )
536 )
garciadeblas2644b762021-03-24 09:21:01 +0100537 raise FsException(
538 "Multiple files found",
539 http_code=HTTPStatus.INTERNAL_SERVER_ERROR,
540 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100541
542 if requested_file.metadata["type"] == "dir":
beierlm3d82ba22022-04-19 14:12:50 -0400543 dir_cursor = self.fs.find(
544 {"filename": {"$regex": "^{}/".format(f)}}
545 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100546
547 for tmp in dir_cursor:
beierlm3d82ba22022-04-19 14:12:50 -0400548 self.logger.debug("Deleting {}".format(tmp.filename))
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100549 self.fs.delete(tmp._id)
beierlm3d82ba22022-04-19 14:12:50 -0400550
551 self.logger.debug("Deleting {}".format(requested_file.filename))
552 self.fs.delete(requested_file._id)
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100553 if not found and not ignore_non_exist:
garciadeblas2644b762021-03-24 09:21:01 +0100554 raise FsException(
555 "File {} does not exist".format(storage),
556 http_code=HTTPStatus.NOT_FOUND,
557 )
Eduardo Sousa0593aba2019-06-04 12:55:43 +0100558 except IOError as e:
garciadeblas2644b762021-03-24 09:21:01 +0100559 raise FsException(
560 "File {} cannot be deleted: {}".format(f, e),
561 http_code=HTTPStatus.INTERNAL_SERVER_ERROR,
562 )
David Garcia788b9d62020-01-20 13:21:06 +0100563
tiernob07e4ef2020-05-06 14:22:48 +0000564 def sync(self, from_path=None):
David Garcia788b9d62020-01-20 13:21:06 +0100565 """
566 Sync from FSMongo to local storage
tiernob07e4ef2020-05-06 14:22:48 +0000567 :param from_path: if supplied, only copy content from this path, not all
568 :return: None
David Garcia788b9d62020-01-20 13:21:06 +0100569 """
lloretgallegf296d2a2020-09-02 09:36:24 +0000570 if from_path:
571 if os.path.isabs(from_path):
572 from_path = os.path.relpath(from_path, self.path)
tiernob07e4ef2020-05-06 14:22:48 +0000573 self.__update_local_fs(from_path=from_path)
lloretgallegf296d2a2020-09-02 09:36:24 +0000574
575 def _update_mongo_fs(self, from_path):
576
577 os_path = self.path + from_path
578
579 # Obtain list of files and dirs in filesystem
580 members = []
581 for root, dirs, files in os.walk(os_path):
582 for folder in dirs:
garciadeblas2644b762021-03-24 09:21:01 +0100583 member = {"filename": os.path.join(root, folder), "type": "dir"}
beierlm945fa222022-04-04 11:44:03 -0400584 if os.path.islink(member["filename"]):
585 member["type"] = "sym"
lloretgallegf296d2a2020-09-02 09:36:24 +0000586 members.append(member)
587 for file in files:
588 filename = os.path.join(root, file)
589 if os.path.islink(filename):
590 file_type = "sym"
591 else:
592 file_type = "file"
garciadeblas2644b762021-03-24 09:21:01 +0100593 member = {"filename": os.path.join(root, file), "type": file_type}
lloretgallegf296d2a2020-09-02 09:36:24 +0000594 members.append(member)
595
596 # Obtain files in mongo dict
597 remote_files = self._get_mongo_files(from_path)
598
599 # Upload members if they do not exists or have been modified
600 # We will do this for performance (avoid updating unmodified files) and to avoid
601 # updating a file with an older one in case there are two sources for synchronization
602 # in high availability scenarios
603 for member in members:
604 # obtain permission
605 mask = int(oct(os.stat(member["filename"]).st_mode)[-3:], 8)
606
607 # convert to relative path
608 rel_filename = os.path.relpath(member["filename"], self.path)
garciadeblas2644b762021-03-24 09:21:01 +0100609 last_modified_date = datetime.datetime.fromtimestamp(
610 os.path.getmtime(member["filename"])
611 )
lloretgallegf296d2a2020-09-02 09:36:24 +0000612
613 remote_file = remote_files.get(rel_filename)
garciadeblas2644b762021-03-24 09:21:01 +0100614 upload_date = (
615 remote_file[0].uploadDate if remote_file else datetime.datetime.min
616 )
lloretgallegf296d2a2020-09-02 09:36:24 +0000617 # remove processed files from dict
618 remote_files.pop(rel_filename, None)
619
620 if last_modified_date >= upload_date:
621
622 stream = None
623 fh = None
624 try:
625 file_type = member["type"]
626 if file_type == "dir":
627 stream = BytesIO()
628 elif file_type == "sym":
garciadeblas2644b762021-03-24 09:21:01 +0100629 stream = BytesIO(
630 os.readlink(member["filename"]).encode("utf-8")
631 )
lloretgallegf296d2a2020-09-02 09:36:24 +0000632 else:
633 fh = open(member["filename"], "rb")
634 stream = BytesIO(fh.read())
635
garciadeblas2644b762021-03-24 09:21:01 +0100636 metadata = {"type": file_type, "permissions": mask}
lloretgallegf296d2a2020-09-02 09:36:24 +0000637
beierlm3d82ba22022-04-19 14:12:50 -0400638 self.logger.debug("Sync upload {}".format(rel_filename))
garciadeblas2644b762021-03-24 09:21:01 +0100639 self.fs.upload_from_stream(rel_filename, stream, metadata=metadata)
lloretgallegf296d2a2020-09-02 09:36:24 +0000640
641 # delete old files
642 if remote_file:
643 for file in remote_file:
beierlm3d82ba22022-04-19 14:12:50 -0400644 self.logger.debug("Sync deleting {}".format(file.filename))
lloretgallegf296d2a2020-09-02 09:36:24 +0000645 self.fs.delete(file._id)
646 finally:
647 if fh:
648 fh.close()
649 if stream:
650 stream.close()
651
652 # delete files that are not any more in local fs
653 for remote_file in remote_files.values():
654 for file in remote_file:
655 self.fs.delete(file._id)
656
657 def _get_mongo_files(self, from_path=None):
658
659 file_dict = {}
garciadeblas2644b762021-03-24 09:21:01 +0100660 file_cursor = self.fs.find(no_cursor_timeout=True, sort=[("uploadDate", -1)])
lloretgallegf296d2a2020-09-02 09:36:24 +0000661 for file in file_cursor:
662 if from_path and not file.filename.startswith(from_path):
663 continue
664 if file.filename in file_dict:
665 file_dict[file.filename].append(file)
666 else:
667 file_dict[file.filename] = [file]
668 return file_dict
669
670 def reverse_sync(self, from_path: str):
671 """
672 Sync from local storage to FSMongo
673 :param from_path: base directory to upload content to mongo fs
674 :return: None
675 """
676 if os.path.isabs(from_path):
677 from_path = os.path.relpath(from_path, self.path)
678 self._update_mongo_fs(from_path=from_path)