예제 #1
0
    def AddTorrentFromFile(self, filepath, forceasklocation = False, dotTorrentDuplicate = False, dest = None, caller = "", label = None):
        if type(filepath) is not unicode:
            filepath = unicode(filepath, sys.getfilesystemencoding())

        # Check to make sure that the source file exists
        sourcefileexists = os.access(filepath, os.R_OK)
        
        if not sourcefileexists:
            errormsg = filepath + \
                       '\n' + \
                       _('Failed : .torrent file does not exist or cannot be read.')
            
            if caller not in ["web", "command"]:
                dialog = wx.MessageDialog(None, 
                                          errormsg,
                                          _('Error'), 
                                          wx.OK|wx.ICON_ERROR)
                result = dialog.ShowModal()
                dialog.Destroy()

            # What do we do if the source file doesn't exist?
            # Just return if the source file doesn't exist?
            return False, errormsg, None

        # Make torrent directory if necessary
        utility.MakeDir("torrent")
      
        torrentpath = os.path.join(utility.getConfigPath(), "torrent")    
        filename     = os.path.split(filepath)[1]
        torrentsrc   = os.path.join(torrentpath, filename)
        dontremove = False
        fileexists = os.access(torrentsrc, os.R_OK)
        
        # If the two files are identical, just point to the
        # .torrent file in the /torrent directory
        sametorrent = isSameTorrent(filepath, torrentsrc)
        if sametorrent:
            filepath = torrentsrc
        else:
            newtorrentsrc = findUniqueFileName(torrentsrc, caller)
            if newtorrentsrc:
                torrentsrc = newtorrentsrc
                dotTorrentDuplicate = True

        # Is the torrent already present in the list?
        torrentinlist = self._checkForDuplicateInList(src = filepath)
        if torrentinlist:
            if caller == "drag":
                return False, "", None
            return self._dupFileInList(filepath, caller), None
        
        if fileexists and not dotTorrentDuplicate:
            errormsg = _('This torrent is a duplicate! \nAre you sure you want to replace it?')
            
            if caller in ["web", "command"]:
                return False, errormsg, None

            # ignore if the src and dest files are the same
            # this means that files in the torrent directory
            # will only give a duplicate torrent error if
            # they are already loaded in the list
            # (dotTorrentDuplicate stays False and the check to
            #  see if it's in the list is performed in _addNewProc)
            ##############################################
            if (filepath == torrentsrc):
                # If _addNewProc finds that the torrent is already in the proctab,
                # we don't want to remove it otherwise the torrent that is running
                # will be in trouble
                dontremove = True
            else:
                # There is a duplicate .torrent file in /torrent
                dialog = wx.MessageDialog(None, 
                                          errormsg,
                                          _('Duplicate torrent'), 
                                          wx.YES_NO|wx.ICON_EXCLAMATION)
                result = dialog.ShowModal()
                dialog.Destroy() 
                if(result == wx.ID_NO):
                    return False, errormsg, None
                else:
                    dotTorrentDuplicate = True
        else:
            # Either:
            # dotTorrentDuplicate was False and the file didn't exist (no change)
            # dotTorrentDuplicate was True before when coming from AddTorrentURL (still need to check the list)
            dotTorrentDuplicate = False

        # No need to copy if we're just copying the file onto itself
        if (filepath != torrentsrc):
            try:
                copy2(filepath, torrentsrc)
            except:
                copy(filepath, torrentsrc)
        return self._addNewProc(torrentsrc, 
                                dest = dest, 
                                forceasklocation = forceasklocation, 
                                dotTorrentDuplicate = dotTorrentDuplicate, 
                                dontremove = dontremove, 
                                caller = caller,
                                new = True,
                                label = label)
예제 #2
0
    def _ScanDir(self):
        self.scandir = utility.config.Read('scandir')

        if not utility.config.Read('setdefaultfolder', "boolean"):
            error = _("Could not start Directory Scanner, you must set a default download folder")
        elif not os.path.isdir(self.scandir):
            error = _("Could not start Directory Scanner, the scan folder is not a directory")
        elif self.scandir == os.path.join(utility.getConfigPath(), 'torrent'):
            error = _("Could not start Directory Scanner, you can't use the default torrent directory")
        else:
            error = None
        if error:
            wx.LogError(error)
            utility.config.Write('scandiractive', "0")
            utility.actions[ACTION_DIRSCANNER].updateButton()
            return

        scanned = [entry for entry in os.listdir(self.scandir) if os.path.isfile(os.path.join(self.scandir, entry))
                   and os.access(os.path.join(self.scandir, entry), os.R_OK)
                   and not entry.startswith(".")
                   and (entry.endswith(".torrent") or entry.endswith(".txt")
                        or os.path.getsize(os.path.join(self.scandir, entry)) < 1024*25)]

        if scanned:
            scanneddir = os.path.join(self.scandir, 'scanned')
            baddir = os.path.join(self.scandir, 'bad')
            
            for file in scanned:
                src = os.path.join(self.scandir, file)
                try:
                    dump1, msg, dump2 = utility.queue.addtorrents.AddTorrentFromFile(src, dotTorrentDuplicate = True, caller = "web")
                except:
                    continue
                    
                if msg == _('OK'):
                    if utility.config.Read('scandirmovetor', "boolean"):
                        moveto = scanneddir
                    else:
                        moveto = None
                    wx.LogMessage('Directory Scanner: %s loaded' % (src))
                elif file.endswith(".torrent"):
                    moveto = baddir
                else:
                    continue
                
                # Move file
                if moveto is None:
                    try:
                        os.remove(src)
                    except:
                        pass
                    continue
                elif not os.path.exists(moveto):
                    os.makedirs(moveto)
                elif os.path.isfile(moveto):
                    continue
                dest = findUniqueFileName(os.path.join(moveto, file), 'scan')
                if dest is None:
                    continue
                try:
                    os.rename(src, dest)
                except:
                    pass