فهرست منبع

added send-by-file

Darren Ranalli 22 سال پیش
والد
کامیت
8242b1c4b7

+ 16 - 4
direct/src/directutil/DistributedLargeBlobSender.py

@@ -45,11 +45,23 @@ class DistributedLargeBlobSender(DistributedObject.DistributedObject):
             self.privOnBlobComplete()
 
     def setFilename(self, filename):
-        DistributedLargeBlobSender.notify.debug('setFilename')
+        DistributedLargeBlobSender.notify.debug('setFilename: %s' % filename)
         assert self.useDisk
-        self.blob = ''
-        DistributedLargeBlobSender.notify.error(
-            'large blob transfer by file not yet implemented')
+
+        import os
+        origDir = os.getcwd()
+        bPath = LargeBlobSenderConsts.getLargeBlobPath()
+        try:
+            os.chdir(bPath)
+        except OSError:
+            DistributedLargeBlobSender.notify.error(
+                'could not access %s' % bPath)
+        f = file(filename, 'rb')
+        self.blob = f.read()
+        f.close()
+        os.unlink(filename)
+        os.chdir(origDir)
+
         self.privOnBlobComplete()
 
     def isComplete(self):

+ 27 - 3
direct/src/directutil/DistributedLargeBlobSenderAI.py

@@ -20,11 +20,35 @@ class DistributedLargeBlobSenderAI(DistributedObjectAI.DistributedObjectAI):
         self.generateWithRequired(zoneId)
 
         # send the data
+        s = str(data)
         if useDisk:
-            DistributedLargeBlobSenderAI.notify.error(
-                'large blob transfer by file not yet implemented')
+            # write the data to a file and tell the client where to get it
+            import os
+            import random
+            origDir = os.getcwd()
+            bPath = LargeBlobSenderConsts.getLargeBlobPath()
+            try:
+                os.chdir(bPath)
+            except OSError:
+                DistributedLargeBlobSenderAI.notify.error(
+                    'could not access %s' % bPath)
+            # find an unused temp filename
+            while 1:
+                num = random.randrange((1 << 30)-1)
+                filename = LargeBlobSenderConsts.FilePattern % num
+                try:
+                    os.stat(filename)
+                except OSError:
+                    break
+            # NOTE: there's a small chance of a race condition here, if
+            # the file is created by another AI just after the stat fails
+            f = file(filename, 'wb')
+            f.write(s)
+            f.close()
+            os.chdir(origDir)
+            self.sendUpdateToAvatarId(self.targetAvId,
+                                      'setFilename', [filename])
         else:
-            s = str(data)
             chunkSize = LargeBlobSenderConsts.ChunkSize
             while len(s):
                 self.sendUpdateToAvatarId(self.targetAvId,

+ 10 - 0
direct/src/directutil/LargeBlobSenderConsts.py

@@ -3,3 +3,13 @@
 USE_DISK  = 0x01
 
 ChunkSize = 100
+
+FilePattern = 'largeBlob.%s'
+
+def getLargeBlobPath():
+    path = config.GetString('large-blob-path', '')
+    if len(path) == 0:
+        assert 0, (
+            'you must config large-blob-path to beta/largeblob, i.e.\n'
+            'large-blob-path i:\\beta\\largeblob')
+    return path