forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
1151 lines (985 loc) · 43.7 KB
/
views.py
File metadata and controls
1151 lines (985 loc) · 43.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import datetime
import os
import uuid
import markupsafe
from urllib.parse import quote
from django.utils import timezone
from flask import make_response
from flask import request
from furl import furl
import jwe
import jwt
from osf.external.gravy_valet.translations import EphemeralNodeSettings
import waffle
from django.db import transaction
from django.contrib.contenttypes.models import ContentType
from elasticsearch6 import exceptions as es_exceptions
from rest_framework import status as http_status
from api.caching.tasks import update_storage_usage_with_size
from addons.base import exceptions as addon_errors
from addons.base.models import BaseStorageAddon
from addons.osfstorage.models import OsfStorageFileNode
from addons.osfstorage.utils import enqueue_update_analytics
from api.waffle.utils import flag_is_active
from framework import sentry
from framework.auth import Auth
from framework.auth import cas
from framework.auth import oauth_scopes
from framework.auth.decorators import collect_auth, must_be_logged_in, must_be_signed
from framework.exceptions import HTTPError
from framework.flask import redirect
from framework.sentry import log_exception
from framework.transactions.handlers import no_auto_transaction
from osf.metrics.es8_metrics import OsfCountedUsageRecord
from website import settings
from addons.base import signals as file_signals
from addons.base.utils import format_last_known_metadata, get_mfr_url
from osf import features
from osf.models import (
BaseFileNode,
TrashedFileNode,
BaseFileVersionsThrough,
OSFUser,
AbstractNode,
Preprint,
Node,
NodeLog,
Registration,
DraftRegistration,
Guid,
FileVersionUserMetadata,
FileVersion, NotificationTypeEnum
)
from osf.metrics import PreprintView, PreprintDownload
from osf.utils import permissions
from osf.external.gravy_valet import request_helpers
from website.profile.utils import get_profile_image_url
from website.project import decorators
from website.project.decorators import must_be_contributor_or_public, must_be_valid_project, check_contributor_auth
from website.project.utils import serialize_node
from website.util import rubeus
# import so that associated listener is instantiated and gets emails
from notifications.file_event_notifications import FileEvent # noqa
ERROR_MESSAGES = {'FILE_GONE': """
<style>
#toggleBar{{display: none;}}
</style>
<div class="alert alert-info" role="alert">
<p>
The file "{file_name}" stored on {provider} was deleted via the OSF.
</p>
<p>
It was deleted by <a href="/{deleted_by_guid}">{deleted_by}</a> on {deleted_on}.
</p>""",
'FILE_GONE_ACTOR_UNKNOWN': """
<style>
#toggleBar{{display: none;}}
</style>
<div class="alert alert-info" role="alert">
<p>
The file "{file_name}" stored on {provider} was deleted via the OSF.
</p>
<p>
It was deleted on {deleted_on}.
</p>""",
'DONT_KNOW': """
<style>
#toggleBar{{display: none;}}
</style>
<div class="alert alert-info" role="alert">
<p>
File not found at {provider}.
</p>""",
'BLAME_PROVIDER': """
<style>
#toggleBar{{display: none;}}
</style>
<div class="alert alert-info" role="alert">
<p>
This {provider} link to the file "{file_name}" is currently unresponsive.
The provider ({provider}) may currently be unavailable or "{file_name}" may have been removed from {provider} through another interface.
</p>
<p>
You may wish to verify this through {provider}'s website.
</p>""",
'FILE_SUSPENDED': """
<style>
#toggleBar{{display: none;}}
</style>
<div class="alert alert-info" role="alert">
This content has been removed."""}
WATERBUTLER_JWE_KEY = jwe.kdf(settings.WATERBUTLER_JWE_SECRET.encode('utf-8'), settings.WATERBUTLER_JWE_SALT.encode('utf-8'))
_READ_ACTIONS = frozenset([
'revisions',
'metadata',
'download',
'render',
'export',
'copyfrom',
])
_WRITE_ACTIONS = frozenset([
'create_folder',
'upload',
'delete',
'copy',
'move',
'copyto',
'moveto',
'movefrom',
])
@decorators.must_have_permission(permissions.WRITE)
@decorators.must_not_be_registration
def disable_addon(auth, **kwargs):
node = kwargs['node'] or kwargs['project']
addon_name = kwargs.get('addon')
if addon_name is None:
raise HTTPError(http_status.HTTP_400_BAD_REQUEST)
deleted = node.delete_addon(addon_name, auth)
return {'deleted': deleted}
@must_be_logged_in
def get_addon_user_config(**kwargs):
user = kwargs['auth'].user
addon_name = kwargs.get('addon')
if addon_name is None:
raise HTTPError(http_status.HTTP_400_BAD_REQUEST)
addon = user.get_addon(addon_name)
if addon is None:
raise HTTPError(http_status.HTTP_400_BAD_REQUEST)
return addon.to_json(user)
def _download_is_from_mfr(waterbutler_data):
metrics_data = waterbutler_data.get('metrics', {})
uri = metrics_data.get('uri', '')
is_render_uri = furl(uri).query.params.get('mode') == 'render'
return (
# This header is sent for download requests that
# originate from MFR, e.g. for the code pygments renderer
request.headers.get('X-Cos-Mfr-Render-Request', None) or
# Need to check the URI in order to account
# for renderers that send XHRs from the
# rendered content, e.g. PDFs
is_render_uri
)
def make_auth(user):
if user is not None:
return {
'id': user._id,
'email': f'{user._id}@osf.io',
'name': user.fullname,
}
return {}
@collect_auth
def get_auth(auth, **kwargs):
"""
Authenticate a request and construct a JWT payload for Waterbutler callbacks.
When a user interacts with a file OSF sends a request to WB which itself sends a
request to an external service or Osfstorage, in order to confirm that event has
taken place Waterbutler will send this callback to OSF to confirm the file action was
successful and can be logged.
This function decrypts and decodes the JWT payload from the request, authenticates
the resource based on the node ID provided in the JWT payload, and ensures the user
has the required permissions to perform the specified action on the node. If the user
is not authenticated, it attempts to authenticate them using the provided data. This
function also constructs a response payload that includes a signed and encrypted JWT,
which Waterbutler can use to verify the request.
Parameters:
auth (Auth): The authentication context, typically collected by the `@collect_auth` decorator.
**kwargs: Keyword arguments that might include additional context needed for complex permissions checks.
Returns:
dict: A dictionary containing the encrypted JWT in 'payload', ready to be used by Waterbutler.
Raises:
HTTPError: If authentication fails, the node does not exist, the action is not allowed, or
any required data for authentication is missing from the request.
"""
waterbutler_data = _decrypt_and_decode_jwt_payload()
resource = _get_authenticated_resource(waterbutler_data['nid'])
action = waterbutler_data['action']
_check_resource_permissions(resource, auth, action)
provider_name = waterbutler_data['provider']
file_version = file_node = None
if provider_name == 'osfstorage' or (not flag_is_active(request, features.ENABLE_GV)):
file_version, file_node = _get_osfstorage_file_version_and_node(
file_path=waterbutler_data.get('path'), file_version_id=waterbutler_data.get('version')
)
waterbutler_settings, waterbutler_credentials = _get_waterbutler_configs(
resource=resource, provider_name=provider_name, file_version=file_version,
)
else:
result = request_helpers.get_waterbutler_config(
gv_addon_pk=f'{waterbutler_data['nid']}:{waterbutler_data['provider']}',
requested_resource=resource,
requesting_user=auth.user,
addon_type='configured-storage-addons',
)
if not result:
raise HTTPError(http_status.HTTP_404_NOT_FOUND, 'Requested Provider is not configured for given node')
waterbutler_settings = result.get_attribute('config')
waterbutler_credentials = result.get_attribute('credentials')
_enqueue_metrics(
file_version=file_version,
file_node=file_node,
action=action,
auth=auth,
from_mfr=_download_is_from_mfr(waterbutler_data),
)
# Construct the response payload including the JWT
return _construct_payload(
auth=auth,
resource=resource,
credentials=waterbutler_credentials,
waterbutler_settings=waterbutler_settings
)
def _decrypt_and_decode_jwt_payload():
try:
payload_encrypted = request.args.get('payload', '').encode('utf-8')
payload_decrypted = jwe.decrypt(payload_encrypted, WATERBUTLER_JWE_KEY)
return jwt.decode(
payload_decrypted,
settings.WATERBUTLER_JWT_SECRET,
options={'require_exp': True},
algorithms=[settings.WATERBUTLER_JWT_ALGORITHM]
)['data']
except (jwt.InvalidTokenError, KeyError) as err:
sentry.log_message(str(err))
raise HTTPError(http_status.HTTP_403_FORBIDDEN)
def _get_authenticated_resource(resource_id):
resource, _ = Guid.load_referent(resource_id)
if not resource:
raise HTTPError(http_status.HTTP_404_NOT_FOUND, message='Resource not found.')
if resource.deleted:
raise HTTPError(http_status.HTTP_410_GONE, message='Resource has been deleted.')
if getattr(resource, 'is_retracted', False):
raise HTTPError(http_status.HTTP_410_GONE, message='Resource has been retracted.')
return resource
def _check_resource_permissions(resource, auth, action):
"""Check if the user has the required permission on the resource."""
required_permission = _get_permission_for_action(action)
_confirm_token_scope(resource, required_permission)
if required_permission == permissions.READ:
has_resource_permissions = resource.can_view_files(auth=auth)
else:
has_resource_permissions = resource.can_edit(auth=auth)
if not (has_resource_permissions or _check_hierarchical_permissions(resource, auth, action)):
raise HTTPError(http_status.HTTP_403_FORBIDDEN)
return True
def _get_permission_for_action(action):
if action in _READ_ACTIONS:
return permissions.READ
if action in _WRITE_ACTIONS:
return permissions.WRITE
raise HTTPError(http_status.HTTP_400_BAD_REQUEST, message='Invalid action specified.')
def _confirm_token_scope(resource, required_permission):
auth_header = request.headers.get('Authorization')
if not (auth_header and auth_header.startswith('Bearer ')):
return # No token, no scope conflict
if required_permission == permissions.READ:
if resource.can_view_files(auth=None):
return # Always allow read actions for public files/valid VOL
required_scope = resource.file_read_scope
else:
required_scope = resource.file_write_scope
if required_scope not in _get_token_scopes_from_cas(auth_header):
raise HTTPError(
http_status.HTTP_403_FORBIDDEN, 'Provided token has insufficient scope for this action'
)
def _get_token_scopes_from_cas(auth_header):
client = cas.get_client()
try:
access_token = cas.parse_auth_header(auth_header)
cas_resp = client.profile(access_token)
except cas.CasError as e:
sentry.log_exception(e)
raise HTTPError(http_status.HTTP_403_FORBIDDEN)
if not cas_resp.authenticated:
raise HTTPError(
http_status.HTTP_403_FORBIDDEN, 'Failed to authenticate via provided Bearer Token'
)
return oauth_scopes.normalize_scopes(cas_resp.attributes.get('accessTokenScope', []))
def _check_hierarchical_permissions(resource, auth, action):
# Users attempting to register projects with components might not have
# `write` permissions for all components. This will result in a 403 for
# all `upload` actions as well as `copyfrom` actions if the component
# in question is not public. To get around this, we have to recursively
# check the node's parent node to determine if they have `write`
# permissions up the stack.
if not isinstance(resource, AbstractNode):
return False
supported_actions = ['copyfrom']
if isinstance(resource, Registration):
supported_actions.append('upload')
if action not in supported_actions:
return False
permissions_resource = resource.parent_node
while permissions_resource:
# Keeping legacy behavior of checking `can_edit` even though `copyfrom` is a READ action
if permissions_resource.can_edit(auth=auth):
return True
permissions_resource = permissions_resource.parent_node
return False
def _get_waterbutler_configs(resource, provider_name, file_version):
try:
addon_settings = resource.serialize_waterbutler_settings(provider_name)
except AttributeError: # No addon configured on resource for provider
raise HTTPError(http_status.HTTP_400_BAD_REQUEST, 'Requested Provider unavailable')
if file_version:
# Override credentials and settings with values for correct storage region
addon_credentials = file_version.region.waterbutler_credentials
addon_settings.update(file_version.region.waterbutler_settings)
else:
addon_credentials = resource.serialize_waterbutler_credentials(provider_name)
return addon_settings, addon_credentials
def _get_osfstorage_file_version_and_node(
file_path: str,
file_version_id: str = None
): # -> tuple[FileVersion, OsfStorageFileNode]
if not file_path:
return None, None
file_node = OsfStorageFileNode.load(file_path.strip('/'))
if not (file_node and file_node.is_file):
return None, None
try:
file_version = FileVersion.objects.select_related('region').get(
basefilenode=file_node,
identifier=file_version_id or str(file_node.versions.count())
)
except FileVersion.DoesNotExist:
raise HTTPError(http_status.HTTP_400_BAD_REQUEST, 'Requested File Version unavailable')
return file_version, file_node
def _enqueue_metrics(file_version, file_node, action, auth, from_mfr=False):
if not file_version:
return
if action == 'render':
file_signals.file_viewed.send(auth=auth, fileversion=file_version, file_node=file_node)
elif action == 'download' and not from_mfr:
file_signals.file_downloaded.send(auth=auth, fileversion=file_version, file_node=file_node)
return
def _construct_payload(auth, resource, credentials, waterbutler_settings):
if isinstance(resource, Registration):
callback_url = resource.callbacks_url
else:
callback_url = resource.api_url_for(
'create_waterbutler_log',
_absolute=True,
_internal=True
)
# Construct the data dictionary for JWT encoding
jwt_data = {
'exp': timezone.now() + datetime.timedelta(seconds=settings.WATERBUTLER_JWT_EXPIRATION),
'data': {
'auth': make_auth(auth.user),
'credentials': credentials,
'settings': waterbutler_settings,
'callback_url': callback_url
}
}
# JWT encode the data
encoded_jwt = jwt.encode(
jwt_data,
settings.WATERBUTLER_JWT_SECRET,
algorithm=settings.WATERBUTLER_JWT_ALGORITHM
)
# Encrypt the encoded JWT with JWE
decoded_encrypted_jwt = jwe.encrypt(
encoded_jwt.encode(),
WATERBUTLER_JWE_KEY
).decode()
return {'payload': decoded_encrypted_jwt}
LOG_ACTION_MAP = {
'move': NodeLog.FILE_MOVED,
'copy': NodeLog.FILE_COPIED,
'rename': NodeLog.FILE_RENAMED,
'create': NodeLog.FILE_ADDED,
'update': NodeLog.FILE_UPDATED,
'delete': NodeLog.FILE_REMOVED,
'create_folder': NodeLog.FOLDER_CREATED,
}
DOWNLOAD_ACTIONS = {
'download_file',
'download_zip',
}
@must_be_signed
@no_auto_transaction
@must_be_valid_project(preprints_valid=True)
def create_waterbutler_log(payload, **kwargs):
with transaction.atomic():
try:
auth = payload['auth']
# Don't log download actions
if payload['action'] in DOWNLOAD_ACTIONS:
guid_id = payload['metadata'].get('nid')
node, _ = Guid.load_referent(guid_id)
return {'status': 'success'}
user = OSFUser.load(auth['id'])
if user is None:
raise HTTPError(http_status.HTTP_400_BAD_REQUEST)
action = LOG_ACTION_MAP[payload['action']]
except KeyError:
raise HTTPError(http_status.HTTP_400_BAD_REQUEST)
auth = Auth(user=user)
node = kwargs.get('node') or kwargs.get('project') or Preprint.load(kwargs.get('nid')) or Preprint.load(kwargs.get('pid'))
if action in (NodeLog.FILE_MOVED, NodeLog.FILE_COPIED):
for bundle in ('source', 'destination'):
for key in ('provider', 'materialized', 'name', 'nid'):
if key not in payload[bundle]:
raise HTTPError(http_status.HTTP_400_BAD_REQUEST)
dest = payload['destination']
src = payload['source']
if src is not None and dest is not None:
dest_path = dest['materialized']
src_path = src['materialized']
if dest_path.endswith('/') and src_path.endswith('/'):
dest_path = os.path.dirname(dest_path)
src_path = os.path.dirname(src_path)
if (
os.path.split(dest_path)[0] == os.path.split(src_path)[0] and
dest['provider'] == src['provider'] and
dest['nid'] == src['nid'] and
dest['name'] != src['name']
):
action = LOG_ACTION_MAP['rename']
destination_node = node # For clarity
source_node = AbstractNode.load(src['nid']) or Preprint.load(src['nid'])
# We return provider fullname so we need to load node settings, if applicable
source = None
if hasattr(source_node, 'get_addon'):
source = source_node.get_addon(payload['source']['provider'])
destination = None
if hasattr(node, 'get_addon'):
destination = node.get_addon(payload['destination']['provider'])
payload['source'].update({
'materialized': payload['source']['materialized'].lstrip('/'),
'addon': source.config.full_name if source else 'osfstorage',
'url': source_node.web_url_for(
'addon_view_or_download_file',
path=payload['source']['path'].lstrip('/'),
provider=payload['source']['provider']
),
'node': {
'_id': source_node._id,
'url': source_node.url,
'title': source_node.title,
}
})
payload['destination'].update({
'materialized': payload['destination']['materialized'].lstrip('/'),
'addon': destination.config.full_name if destination else 'osfstorage',
'url': destination_node.web_url_for(
'addon_view_or_download_file',
path=payload['destination']['path'].lstrip('/'),
provider=payload['destination']['provider']
),
'node': {
'_id': destination_node._id,
'url': destination_node.url,
'title': destination_node.title,
}
})
if not payload.get('errors'):
destination_node.add_log(
action=action,
auth=auth,
params=payload
)
if payload.get('email') or payload.get('errors'):
if payload.get('email'):
notification_type = NotificationTypeEnum.USER_FILE_OPERATION_SUCCESS.instance
if payload.get('errors'):
notification_type = NotificationTypeEnum.USER_FILE_OPERATION_FAILED.instance
notification_type.emit(
user=user,
subscribed_object=node,
event_context={
'action': payload['action'],
'source_node': source_node._id,
'source_node_title': source_node.title,
'destination_node': destination_node._id,
'destination_node_title': destination_node.title,
'destination_node_parent_node_title': destination_node.parent_node.title if destination_node.parent_node else None,
'source_path': payload['source']['materialized'],
'source_addon': payload['source']['addon'],
'destination_addon': payload['destination']['addon'],
'osf_support_email': settings.OSF_SUPPORT_EMAIL,
'logo': settings.OSF_LOGO,
'OSF_LOGO_LIST': settings.OSF_LOGO_LIST,
'OSF_LOGO': settings.OSF_LOGO,
'domain': settings.DOMAIN,
}
)
if payload.get('errors'):
# Action failed but our function succeeded
# Bail out to avoid file_signals
return {'status': 'success'}
else:
node.create_waterbutler_log(auth, action, payload)
metadata = payload.get('metadata') or payload.get('destination')
target_node = AbstractNode.load(metadata.get('nid'))
if target_node and payload['action'] != 'download_file':
update_storage_usage_with_size(payload)
with transaction.atomic():
file_signals.file_updated.send(target=node, user=user, event_type=action, payload=payload)
return {'status': 'success'}
@file_signals.file_updated.connect
def addon_delete_file_node(self, target, user, event_type, payload):
""" Get addon BaseFileNode(s), move it into the TrashedFileNode collection
and remove it from StoredFileNode.
Required so that the guids of deleted addon files are not re-pointed when an
addon file or folder is moved or renamed.
"""
if event_type == 'file_removed' and payload.get('provider', None) != 'osfstorage':
provider = payload['provider']
path = payload['metadata']['path']
materialized_path = payload['metadata']['materialized']
content_type = ContentType.objects.get_for_model(target)
if path.endswith('/'):
folder_children = BaseFileNode.resolve_class(provider, BaseFileNode.ANY).objects.filter(
provider=provider,
target_object_id=target.id,
target_content_type=content_type,
_materialized_path__startswith=materialized_path
)
for item in folder_children:
if item.kind == 'file' and not TrashedFileNode.load(item._id):
item.delete(user=user)
elif item.kind == 'folder':
BaseFileNode.delete(item)
else:
try:
file_node = BaseFileNode.resolve_class(provider, BaseFileNode.FILE).objects.get(
target_object_id=target.id,
target_content_type=content_type,
_materialized_path=materialized_path
)
except BaseFileNode.DoesNotExist:
file_node = None
if file_node and not TrashedFileNode.load(file_node._id):
file_node.delete(user=user)
@file_signals.file_viewed.connect
def osfstoragefile_mark_viewed(self, auth, fileversion, file_node):
if auth.user:
# mark fileversion as seen
FileVersionUserMetadata.objects.get_or_create(user=auth.user, file_version=fileversion)
@file_signals.file_viewed.connect
def osfstoragefile_update_view_analytics(self, auth, fileversion, file_node):
resource = file_node.target
user = getattr(auth, 'user', None)
if hasattr(resource, 'is_contributor_or_group_member') and resource.is_contributor_or_group_member(user):
# Don't record views by contributors
return
enqueue_update_analytics(
resource,
file_node,
fileversion.identifier,
'view'
)
@file_signals.file_viewed.connect
def osfstoragefile_viewed_update_metrics(self, auth, fileversion, file_node):
resource = file_node.target
user = getattr(auth, 'user', None)
if hasattr(resource, 'is_contributor_or_group_member') and resource.is_contributor_or_group_member(user):
# Don't record views by contributors
return
if waffle.switch_is_active(features.ELASTICSEARCH_METRICS) and isinstance(resource, Preprint):
try:
PreprintView.record_for_preprint(
preprint=resource,
user=auth.user,
version=fileversion.identifier,
path=file_node.path,
)
OsfCountedUsageRecord.record(
user_id=getattr(user, '_id', None),
item_osfid=resource._id,
action_labels=[
OsfCountedUsageRecord.ActionLabel.VIEW.value,
OsfCountedUsageRecord.ActionLabel.WEB.value,
],
# HACK: we don't have the user request, so fabricate a one-off session id
# (this means no double-click filtering and inflated "unique" view counts)
client_session_id=str(uuid.uuid4()),
)
except es_exceptions.ConnectionError:
log_exception()
@file_signals.file_downloaded.connect
def osfstoragefile_downloaded_update_analytics(self, auth, fileversion, file_node):
resource = file_node.target
if not resource.is_contributor_or_group_member(auth.user):
version_index = int(fileversion.identifier) - 1
enqueue_update_analytics(resource, file_node, version_index, 'download')
@file_signals.file_downloaded.connect
def osfstoragefile_downloaded_update_metrics(self, auth, fileversion, file_node):
resource = file_node.target
user = getattr(auth, 'user', None)
if hasattr(resource, 'is_contributor_or_group_member') and resource.is_contributor_or_group_member(user):
# Don't record downloads by contributors
return
if waffle.switch_is_active(features.ELASTICSEARCH_METRICS) and isinstance(resource, Preprint):
try:
PreprintDownload.record_for_preprint(
preprint=resource,
user=auth.user,
version=fileversion.identifier,
path=file_node.path,
)
OsfCountedUsageRecord.record(
user_id=getattr(user, '_id', None),
item_osfid=resource._id,
action_labels=[
OsfCountedUsageRecord.ActionLabel.DOWNLOAD.value,
],
# HACK: we don't have the user request, so fabricate a one-off session id
# (this means no double-click filtering and inflated "unique" download counts)
client_session_id=str(uuid.uuid4()),
)
except es_exceptions.ConnectionError:
log_exception()
@must_be_valid_project
def addon_view_or_download_file_legacy(**kwargs):
query_params = request.args.to_dict()
node = kwargs.get('node') or kwargs['project']
action = query_params.pop('action', 'view')
provider = kwargs.get('provider', 'osfstorage')
if kwargs.get('path'):
path = kwargs['path']
elif kwargs.get('fid'):
path = kwargs['fid']
if 'download' in request.path or request.path.startswith('/api/v1/'):
action = 'download'
if kwargs.get('vid'):
query_params['version'] = kwargs['vid']
# If provider is OSFstorage, check existence of requested file in the filetree
# This prevents invalid GUIDs from being created
if provider == 'osfstorage':
node_settings = node.get_addon('osfstorage')
try:
path = node_settings.get_root().find_child_by_name(path)._id
except OsfStorageFileNode.DoesNotExist:
raise HTTPError(
404, data=dict(
message_short='File not found',
message_long='You requested a file that does not exist.'
)
)
return redirect(
node.web_url_for(
'addon_view_or_download_file',
path=path,
provider=provider,
action=action,
**query_params
),
code=http_status.HTTP_301_MOVED_PERMANENTLY
)
@must_be_contributor_or_public
def addon_deleted_file(auth, target, error_type='BLAME_PROVIDER', **kwargs):
"""Shows a nice error message to users when they try to view a deleted file
"""
# Allow file_node to be passed in so other views can delegate to this one
file_node = kwargs.get('file_node') or TrashedFileNode.load(kwargs.get('trashed_id'))
deleted_by, deleted_on, deleted = None, None, None
if isinstance(file_node, TrashedFileNode):
deleted_by = file_node.deleted_by
deleted_by_guid = file_node.deleted_by._id if deleted_by else None
deleted_on = file_node.deleted_on.strftime('%c') + ' UTC'
deleted = deleted_on
if getattr(file_node, 'suspended', False):
error_type = 'FILE_SUSPENDED'
elif file_node.deleted_by is None or (auth.private_key and auth.private_link.anonymous):
if file_node.provider == 'osfstorage':
error_type = 'FILE_GONE_ACTOR_UNKNOWN'
else:
error_type = 'BLAME_PROVIDER'
else:
error_type = 'FILE_GONE'
else:
error_type = 'DONT_KNOW'
file_path = kwargs.get('path', file_node.path)
file_name = file_node.name or os.path.basename(file_path)
file_name_title, file_name_ext = os.path.splitext(file_name)
provider_full = settings.ADDONS_AVAILABLE_DICT[file_node.provider].full_name
try:
file_guid = file_node.get_guid()._id
except AttributeError:
file_guid = None
format_params = dict(
file_name=markupsafe.escape(file_name),
deleted_by=markupsafe.escape(getattr(deleted_by, 'fullname', None)),
deleted_on=markupsafe.escape(deleted_on),
provider=markupsafe.escape(provider_full),
deleted=markupsafe.escape(deleted)
)
if deleted_by:
format_params['deleted_by_guid'] = markupsafe.escape(deleted_by_guid)
error_msg = ERROR_MESSAGES[error_type].format(**format_params)
if isinstance(target, AbstractNode):
error_msg += format_last_known_metadata(auth, target, file_node, error_type)
ret = serialize_node(target, auth, primary=True)
ret.update(rubeus.collect_addon_assets(target))
ret.update({
'error': error_msg,
'urls': {
'render': None,
'sharejs': None,
'mfr': get_mfr_url(target, file_node.provider),
'profile_image': get_profile_image_url(auth.user, 25),
'files': target.web_url_for('collect_file_trees'),
},
'extra': {},
'size': 9966699, # Prevent file from being edited, just in case
'sharejs_uuid': None,
'file_name': file_name,
'file_path': file_path,
'file_name_title': file_name_title,
'file_name_ext': file_name_ext,
'target_deleted': getattr(target, 'is_deleted', False),
'version_id': None,
'file_guid': file_guid,
'file_id': file_node._id,
'provider': file_node.provider,
'materialized_path': file_node.materialized_path or file_path,
'private': getattr(target.get_addon(file_node.provider), 'is_private', False),
'file_tags': list(file_node.tags.filter(system=False).values_list('name', flat=True)) if not file_node._state.adding else [], # Only access ManyRelatedManager if saved
'allow_comments': file_node.provider in settings.ADDONS_COMMENTABLE,
})
else:
# TODO - serialize deleted metadata for future types of deleted file targets
ret = {'error': error_msg}
return ret, http_status.HTTP_410_GONE
@must_be_contributor_or_public
def addon_view_or_download_file(auth, path, provider, **kwargs):
extras = request.args.to_dict()
extras.pop('_', None) # Clean up our url params a bit
action = extras.get('action', 'view')
guid = kwargs.get('guid')
guid_target, _ = Guid.load_referent(guid)
target = guid_target or kwargs.get('node') or kwargs['project']
provider_safe = markupsafe.escape(provider)
path_safe = markupsafe.escape(path)
if not path:
raise HTTPError(http_status.HTTP_400_BAD_REQUEST)
if hasattr(target, 'get_addon'):
node_addon = target.get_addon(provider)
if flag_is_active(request, features.ENABLE_GV):
if not isinstance(node_addon, EphemeralNodeSettings) and provider != 'osfstorage':
object_text = markupsafe.escape(getattr(target, 'project_or_component', 'this object'))
raise HTTPError(http_status.HTTP_400_BAD_REQUEST, data={
'message_short': 'Bad Request',
'message_long': f'The {provider_safe} add-on containing {path_safe} is no longer connected to {object_text}.'
})
elif not isinstance(node_addon, BaseStorageAddon):
object_text = markupsafe.escape(getattr(target, 'project_or_component', 'this object'))
raise HTTPError(http_status.HTTP_400_BAD_REQUEST, data={
'message_short': 'Bad Request',
'message_long': f'The {provider_safe} add-on containing {path_safe} is no longer connected to {object_text}.'
})
if not node_addon.has_auth:
raise HTTPError(http_status.HTTP_401_UNAUTHORIZED, data={
'message_short': 'Unauthorized',
'message_long': f'The {provider_safe} add-on containing {path_safe} is no longer authorized.'
})
if not node_addon.complete:
raise HTTPError(http_status.HTTP_400_BAD_REQUEST, data={
'message_short': 'Bad Request',
'message_long': f'The {provider_safe} add-on containing {path_safe} is no longer configured.'
})
savepoint_id = transaction.savepoint()
try:
file_node = BaseFileNode.resolve_class(
provider, BaseFileNode.FILE
).get_or_create(
target, path, **extras
)
except addon_errors.QueryError as e:
raise HTTPError(
http_status.HTTP_400_BAD_REQUEST,
data={
'message_short': 'Bad Request',
'message_long': str(e)
}
)
except addon_errors.DoesNotExist as e:
raise HTTPError(
http_status.HTTP_404_NOT_FOUND,
data={
'message_short': 'Not Found',
'message_long': str(e)
}
)
# Note: Cookie is provided for authentication to waterbutler
# it is overridden to force authentication as the current user
# the auth header is also pass to support basic auth
version = file_node.touch(
request.headers.get('Authorization'),
**dict(
extras,
cookie=request.cookies.get(settings.COOKIE_NAME)
)
)
# There's no download action redirect to the Ember front-end file view and create guid.
if action != 'download':
if isinstance(target, Node) and flag_is_active(request, features.EMBER_FILE_PROJECT_DETAIL):
guid = file_node.get_guid(create=True)
return redirect(f'{settings.DOMAIN}{guid._id}/')
if isinstance(target, Registration) and flag_is_active(request, features.EMBER_FILE_REGISTRATION_DETAIL):
guid = file_node.get_guid(create=True)
return redirect(f'{settings.DOMAIN}{guid._id}/')
if version is None:
# File is either deleted or unable to be found in the provider location
# Rollback the insertion of the file_node
transaction.savepoint_rollback(savepoint_id)
if not file_node.pk:
file_node = BaseFileNode.load(path)
if not file_node:
raise HTTPError(http_status.HTTP_404_NOT_FOUND, data={
'message_short': 'File Not Found',
'message_long': 'The requested file could not be found.'
})
if file_node.kind == 'folder':
raise HTTPError(http_status.HTTP_400_BAD_REQUEST, data={
'message_short': 'Bad Request',
'message_long': 'You cannot request a folder from this endpoint.'
})
# Allow osfstorage to redirect if the deep url can be used to find a valid file_node
if file_node.provider == 'osfstorage' and not file_node.is_deleted:
return redirect(
file_node.target.web_url_for('addon_view_or_download_file', path=file_node._id, provider=file_node.provider)
)
return addon_deleted_file(target=target, file_node=file_node, path=path, **kwargs)
else:
transaction.savepoint_commit(savepoint_id)
# TODO clean up these urls and unify what is used as a version identifier
if request.method == 'HEAD':
return make_response(('', http_status.HTTP_302_FOUND, {
'Location': file_node.generate_waterbutler_url(**dict(extras, direct=None, version=version.identifier, _internal=extras.get('mode') == 'render'))
}))
if action == 'download':
format = extras.get('format')
_, extension = os.path.splitext(file_node.name)
# avoid rendering files with the same format type.
if format and f'.{format.lower()}' != extension.lower():