@@ -2618,10 +2618,10 @@ class ResourceFile(ResourceFileIRODSMixin):
26182618 fed_resource_file = models .FileField (upload_to = get_path , max_length = 4096 ,
26192619 null = True , blank = True , storage = FedStorage ())
26202620
2621- # DEPRECATED: utilize resfile.set_storage_path( path) and resfile.storage_path.
2622- # fed_resource_file_name_or_path = models.CharField(max_length=255, null=True, blank=True)
2623- # DEPRECATED: use native size routine
2624- # fed_resource_file_size = models.CharField(max_length=15, null=True, blank=True)
2621+ # This is used to hold the reference path to an external file, e.g., a logical iRODS
2622+ # path refering to a file stored in an external iRODS zone, a URL that points to an external file
2623+ reference_file_path = models . CharField ( max_length = 255 , null = True , blank = True )
2624+ reference_file_size = models .CharField (max_length = 15 , null = True , blank = True )
26252625
26262626 # we are using GenericForeignKey to allow resource file to be associated with any
26272627 # CommonsShare defined LogicalFile types (e.g., GeoRasterFile, NetCdfFile etc)
@@ -2640,7 +2640,7 @@ def __str__(self):
26402640 return self .resource_file .name
26412641
26422642 @classmethod
2643- def create (cls , resource , file , folder = None , source = None , move = False ):
2643+ def create (cls , resource , file , folder = None , source = None , source_size = 0 , is_file_reference = False , move = False ):
26442644 """Create custom create method for ResourceFile model.
26452645
26462646 Create takes arguments that are invariant of storage medium.
@@ -2651,6 +2651,9 @@ def create(cls, resource, file, folder=None, source=None, move=False):
26512651 :param file: a File or a iRODS path to an existing file already copied.
26522652 :param folder: the folder in which to store the file.
26532653 :param source: an iRODS path in the same zone from which to copy the file.
2654+ :param source_size: the size of the referenced source is if_file_reference is True, otherwise, it is set to 0
2655+ and useless.
2656+ :param is_file_reference: if True, source will hold the reference path to an external file
26542657 :param move: if True, move the file rather than copying.
26552658
26562659 There are two main usages to this constructor:
@@ -2697,23 +2700,31 @@ def create(cls, resource, file, folder=None, source=None, move=False):
26972700 if file is None and source is not None :
26982701 if __debug__ :
26992702 assert (isinstance (source , basestring ))
2700- # source is a path to an iRODS file to be copied here.
2701- root , newfile = os .path .split (source ) # take file from source path
2702- # newfile is where it should be copied to.
2703- target = get_resource_file_path (resource , newfile , folder = folder )
2704- istorage = resource .get_irods_storage ()
2705- if not istorage .exists (source ):
2706- raise ValidationError ("ResourceFile.create: source {} of copy not found"
2707- .format (source ))
2708- if not move :
2709- istorage .copyFiles (source , target )
2703+
2704+ if is_file_reference :
2705+ kwargs ['resource_file' ] = None
2706+ kwargs ['fed_resource_file' ] = None
2707+ kwargs ['reference_file_path' ] = source
2708+ kwargs ['reference_file_size' ] = str (source_size )
2709+ return ResourceFile .objects .create (** kwargs )
27102710 else :
2711- istorage .moveFile (source , target )
2712- if not istorage .exists (target ):
2713- raise ValidationError ("ResourceFile.create: copy to target {} failed"
2714- .format (target ))
2715- if move and istorage .exists (source ):
2716- raise ValidationError ("ResourceFile.create: move did not work" )
2711+ # source is a path to an iRODS file to be copied here.
2712+ root , newfile = os .path .split (source ) # take file from source path
2713+ # newfile is where it should be copied to.
2714+ target = get_resource_file_path (resource , newfile , folder = folder )
2715+ istorage = resource .get_irods_storage ()
2716+ if not istorage .exists (source ):
2717+ raise ValidationError ("ResourceFile.create: source {} of copy not found"
2718+ .format (source ))
2719+ if not move :
2720+ istorage .copyFiles (source , target )
2721+ else :
2722+ istorage .moveFile (source , target )
2723+ if not istorage .exists (target ):
2724+ raise ValidationError ("ResourceFile.create: copy to target {} failed"
2725+ .format (target ))
2726+ if move and istorage .exists (source ):
2727+ raise ValidationError ("ResourceFile.create: move did not work" )
27172728 elif file is not None and source is None :
27182729 # file points to an existing iRODS file
27192730 # no need to verify whether the file exists in iRODS since the file
@@ -2766,11 +2777,13 @@ def size(self):
27662777 assert self .resource_file .name is None or \
27672778 self .resource_file .name == ''
27682779 return self .fed_resource_file .size
2769- else :
2780+ elif not self . reference_file_path :
27702781 if __debug__ :
27712782 assert self .fed_resource_file .name is None or \
27722783 self .fed_resource_file .name == ''
27732784 return self .resource_file .size
2785+ else :
2786+ return 0
27742787
27752788 # TODO: write unit test
27762789 @property
@@ -2874,6 +2887,8 @@ def short_path(self):
28742887 """
28752888 if self .resource .is_federated :
28762889 folder , base = self .path_is_acceptable (self .fed_resource_file .name , test_exists = False )
2890+ elif self .reference_file_path :
2891+ return self .reference_file_path
28772892 else :
28782893 folder , base = self .path_is_acceptable (self .resource_file .name , test_exists = False )
28792894 if folder is not None :
@@ -2895,7 +2910,7 @@ def set_short_path(self, path):
28952910 if self .resource .is_federated :
28962911 self .resource_file = None
28972912 self .fed_resource_file = get_path (self , base )
2898- else :
2913+ elif not self . reference_file_path :
28992914 self .resource_file = get_path (self , base )
29002915 self .fed_resource_file = None
29012916 self .save ()
0 commit comments