Beispiel #1
0
def addFiles(files):
    print "have to check %d files" % len(files)
    errors = []
    base = re.compile("^(.*)\.([\w]+)$")

    for file in files:
        if isinstance(file, File): continue
        f,revision = getFile(file)
        if not f: 
            print "%s not found!" % file
            continue
        path = f['filepath']
        f['id'] = f['fid']
        f['filepath'] = f['filename']
        f['filename'] = revision['description']
        f['node'] = f['nid']
        del(f['fid'])
        del(f['nid'])
        
        try:
            File.get(f['id'])
            print "file %s already there" % f['id']
            continue
        except SQLObjectNotFound:
            file = File(**f)
            
        match = base.match(file.filepath)
        if not match: 
            print "%s odd filename" % file.filename
            continue
        
        basename,ext = match.groups()
        
        if file.filemime[:5] == "video":
            download_from = "http://master/files/orbit/%s.flv" % urllib.quote(basename)
            download_to = "%s/movies/%s.flv" % (DOWNLOAD,basename)
            filename = basename + ".flv"
            alt = "%s/files/orbit/%s" % (DOWNLOAD,filename)
        
        elif file.filemime[:5] == "image":
            download_from = "http://master/%s" % urllib.quote(path)
            filename = basename + "." + ext
            download_to = "%s/images/%s" % (DOWNLOAD,filename)
            alt = "%s/%s" % (DOWNLOAD,path)            

        elif file.filemime[:5] == "audio":
            download_from = "http://master/%s" % urllib.quote(path)
            filename = basename + "." + ext
            download_to = "%s/audio/%s" % (DOWNLOAD,filename)
            alt = "%s/%s" % (DOWNLOAD,path)
    
        if not os.path.isfile(download_to) and os.path.isfile(alt):
            print "copy %s" % basename 
            shutil.copy(alt,download_to)
        elif not os.path.isfile(download_to):
            print "download %s" % basename 
            urllib.urlretrieve(download_from, download_to)
    
        if not os.path.isfile(download_to):
            print "could not fetch %s!" % download_to
            errors.append(file)
            continue
        
        if file.filemime[:5] == "image":
            if file.filemime[6:] == "gif":
                print "convert gif"
                movie = '%s/movies/%s.flv' % (DOWNLOAD,basename)
                cmd1 = "ffmpeg -f gif -i '%s' -s 320x240 -y %s" % (download_to,movie)
                os.popen(cmd1)
                if not os.path.isfile(movie):
                    print "%s cont execute!" % cmd1
                    errors.append(file)
                    continue
                else: 
                    os.unlink(download_to)
                file.filemime = "video/flv"
                filename = basename + ".flv"
            else:
                download_to_old = ""
                if file.filemime[6:] != "jpeg":
                    download_to_old = download_to
                    download_to = download_to + ".jpg"
                    filename = filename + ".jpg"
                    shutil.move(download_to_old,download_to)
                    
                cmd1 = "sips --getProperty pixelWidth '%s'" % download_to
                cmd2 = "sips --setProperty format jpeg --resampleWidth 400 '%s'" % download_to
                
                try:
                    if int(os.popen(cmd1).readlines()[1][14:-1]) <> 400:
                        os.popen(cmd2)
                        file.filemime = "image/jpeg"
                except IndexError:
                    print "%s cont execute!" % cmd2
                    errors.append(file)
                    continue
            
        file.filepath = filename

    for e in errors:
        e.destroySelf()