Ejemplo n.º 1
0
	def __init__(self, submission):
		from InformaCamModels.submission import Submission
		from InformaCamModels.collection import Collection
		
		# unzip
		self.asset_path = submission.asset_path.replace("/submissions/","/collections/")
		try:
			os.makedirs(self.asset_path)
		except OSError as e:
			print e
		
		print submission.emit()
		unzip = ShellThreader([
			"unzip", 
			"%s/%s" % (submission.asset_path, submission.file_name),
			"-d", self.asset_path
		])
		unzip.start()
		unzip.join()
		print unzip.output
		
		'''		
		unzip_output = ['Archive:  /home/ubuntu/assets/submissions/ff4d54bbbceb4ca95106d85e2f38e8e9/0B_D7fbraOf5HeXpDV0VCM0dRQVk.j3mlog', '  inflating: /home/ubuntu/assets/collections/ff4d54bbbceb4ca95106d85e2f38e8e9/log.j3m  ', '  inflating: /home/ubuntu/assets/collections/ff4d54bbbceb4ca95106d85e2f38e8e9/1387543600423_selfie-1943498071.jpg  ', '  inflating: /home/ubuntu/assets/collections/ff4d54bbbceb4ca95106d85e2f38e8e9/informaCaches/fa2f38e4faa783c7049641f600b4d308  ', '  inflating: /home/ubuntu/assets/collections/ff4d54bbbceb4ca95106d85e2f38e8e9/1387543600401_selfie-82254992.jpg  ']
		'''

		assets = []
		
		media = []
		sensor_captures = []
		j3m_verified = False
		
		rx = r'\s*inflating: (.*)'
		for line in unzip.output:
		#for line in unzip_output:
			asset_match = re.findall(rx, line)
			if len(asset_match) == 1:
				assets.append(asset_match[0].strip())
				
		r_j3m = r'.*/log.j3m'
		r_caches = r'.*/informaCaches/.*'
		
		for asset in assets:
			j3m_match = re.match(r_j3m, asset)
			if j3m_match is not None:
				print "J3M: %s" % asset
				j3m_verified = self.validateManifest()
				
			cache_match = re.match(r_caches, asset)
			if cache_match is not None:
				print "ADD CACHE: %s" %  asset
				sensor_captures.append(asset)
			
			else:
				mime_type = getMimeTypeFromFile(asset)
				print mime_type
				if mime_type in [mime_types['image'], mime_types['video']]:
					print "HERE IS A NEW SUB:%s" % asset
					
					f = open(asset, 'rb')
					data = {
						'_id' : hashString(asset),
						'file_name' : os.path.basename(asset),
						'mime_type' : mime_type,
						'package_content' : b64encode(f.read()).rstrip("="),
						'sync_source' : submission.sync_source
					}
					f.close()
					
					if data['file_name'][-4:] != ".%s" % mime_type_map[mime_type]:
						data['file_name'] = "%s.%s" % (
							data['file_name'], mime_type_map[mime_type])
					
					try:
						new_submission = Submission(inflate=data)
						media.append(new_submission._id)
					except exceptions.ConnectionError as e:
						print e
						return
		
		'''
Ejemplo n.º 2
0
    def __init__(self, submission):
        from InformaCamModels.submission import Submission
        from InformaCamModels.collection import Collection
        from InformaCamModels.j3m import J3M

        # unzip
        self.asset_path = submission.asset_path.replace(
            "/submissions/", "/collections/")
        try:
            os.makedirs(self.asset_path)
        except OSError as e:
            print e

        print submission.emit()
        unzip = ShellThreader([
            "unzip",
            "%s/%s" % (submission.asset_path, submission.file_name), "-d",
            self.asset_path
        ])
        unzip.start()
        unzip.join()
        print unzip.output

        assets = []
        media = []
        sensor_captures = []
        j3m_verified = False
        j3m = None

        rx = r'\s*inflating: (.*)'
        for line in unzip.output:
            #for line in unzip_output:
            asset_match = re.findall(rx, line)
            if len(asset_match) == 1:
                assets.append(asset_match[0].strip())

        r_j3m = r'.*/log.j3m'
        r_caches = r'.*/informaCaches/.*'

        for asset in assets:
            j3m_match = re.match(r_j3m, asset)
            if j3m_match is not None:
                print "J3M: %s" % asset
                j3m_verified = self.validateManifest()
                j3m = J3M(path_to_j3m="%s.manifest" % asset)

            cache_match = re.match(r_caches, asset)
            if cache_match is not None:
                print "ADD CACHE: %s" % asset
                sensor_captures.append(asset)

            else:
                mime_type = getMimeTypeFromFile(asset)
                print mime_type
                if mime_type in [mime_types['image'], mime_types['video']]:
                    print "HERE IS A NEW SUB:%s" % asset

                    f = open(asset, 'rb')
                    data = {
                        '_id': hashString(asset),
                        'file_name': os.path.basename(asset),
                        'mime_type': mime_type,
                        'package_content': b64encode(f.read()).rstrip("="),
                        'sync_source': submission.sync_source
                    }
                    f.close()

                    if data['file_name'][
                            -4:] != ".%s" % mime_type_map[mime_type]:
                        data['file_name'] = "%s.%s" % (
                            data['file_name'], mime_type_map[mime_type])

                    try:
                        new_submission = Submission(inflate=data)
                        media.append(new_submission._id)
                    except exceptions.ConnectionError as e:
                        print e
                        return

        inflate = {'_id': submission._id, 'asset_path': self.asset_path}

        if len(media) > 0:
            inflate['attached_media'] = media

        if len(sensor_captures) > 0:
            inflate['sensor_captures'] = sensor_captures

        if j3m is not None:
            inflate['j3m_id'] = j3m._id
            inflate['dateCreated'] = j3m.genealogy['dateCreated']

        collection = Collection(inflate=inflate)
        print "NEW COLLECTION: %s" % collection._id

        # delete self as submission
        submission.delete()
Ejemplo n.º 3
0
    def __init__(self, submission):
        from InformaCamModels.submission import Submission
        from InformaCamModels.collection import Collection
        from InformaCamModels.j3m import J3M

        # unzip
        self.asset_path = submission.asset_path.replace("/submissions/", "/collections/")
        try:
            os.makedirs(self.asset_path)
        except OSError as e:
            print e

        print submission.emit()
        unzip = ShellThreader(["unzip", "%s/%s" % (submission.asset_path, submission.file_name), "-d", self.asset_path])
        unzip.start()
        unzip.join()
        print unzip.output

        assets = []
        media = []
        sensor_captures = []
        j3m_verified = False
        j3m = None

        rx = r"\s*inflating: (.*)"
        for line in unzip.output:
            # for line in unzip_output:
            asset_match = re.findall(rx, line)
            if len(asset_match) == 1:
                assets.append(asset_match[0].strip())

        r_j3m = r".*/log.j3m"
        r_caches = r".*/informaCaches/.*"

        for asset in assets:
            j3m_match = re.match(r_j3m, asset)
            if j3m_match is not None:
                print "J3M: %s" % asset
                j3m_verified = self.validateManifest()
                j3m = J3M(path_to_j3m="%s.manifest" % asset)

            cache_match = re.match(r_caches, asset)
            if cache_match is not None:
                print "ADD CACHE: %s" % asset
                sensor_captures.append(asset)

            else:
                mime_type = getMimeTypeFromFile(asset)
                print mime_type
                if mime_type in [mime_types["image"], mime_types["video"]]:
                    print "HERE IS A NEW SUB:%s" % asset

                    f = open(asset, "rb")
                    data = {
                        "_id": hashString(asset),
                        "file_name": os.path.basename(asset),
                        "mime_type": mime_type,
                        "package_content": b64encode(f.read()).rstrip("="),
                        "sync_source": submission.sync_source,
                    }
                    f.close()

                    if data["file_name"][-4:] != ".%s" % mime_type_map[mime_type]:
                        data["file_name"] = "%s.%s" % (data["file_name"], mime_type_map[mime_type])

                    try:
                        new_submission = Submission(inflate=data)
                        media.append(new_submission._id)
                    except exceptions.ConnectionError as e:
                        print e
                        return

        inflate = {"_id": submission._id, "asset_path": self.asset_path}

        if len(media) > 0:
            inflate["attached_media"] = media

        if len(sensor_captures) > 0:
            inflate["sensor_captures"] = sensor_captures

        if j3m is not None:
            inflate["j3m_id"] = j3m._id
            inflate["dateCreated"] = j3m.genealogy["dateCreated"]

        collection = Collection(inflate=inflate)
        print "NEW COLLECTION: %s" % collection._id

        # delete self as submission
        submission.delete()