Пример #1
0
def add_media_to_db(path):
    # when loading, media is in media_tmp
    match = re.search(r"\d", path)
    if match is None:
        print("could not find post id in file path!")
        return

    digit = match.start()
    entry_id = path[digit:]

    media_folder = os.path.abspath("media_tmp")
    pattern = os.path.join(media_folder, f"{entry_id}.*")

    actual_path = os.path.join("media_tmp", entry_id)
    if not os.path.exists(actual_path):
        print(f"was asked to add {actual_path} to db, but does not exist!")
        return
    file_hash = get_file_hash(actual_path)

    entry = MediaEntry(id=entry_id, file_hash=file_hash)
    data = b''

    db.session.add(entry)
    db.session.commit()

    if Media.query.get(file_hash) is not None:
        return

    size = os.stat(actual_path).st_size
    with open(actual_path, "rb") as file:
        data = file.read(size)
    media = Media(file_hash=file_hash, data=data)

    db.session.add(media)
    db.session.commit()
Пример #2
0
def load_freesound_audio():
    """Load points gathered from Freesound API"""

    print "Freesound SF"

    for row in open("seed_data/freesounds.txt"):
        row = row.rstrip()

        latitude, longitude, title, details, audio_url = row.split('|')

        tag = Tag(latitude=latitude,
                  longitude=longitude,
                  title=title,
                  details=details)

        db.session.add(tag)
        db.session.commit()

        tag_genre = TagGenre(tag_id=tag.tag_id, genre="audio")

        db.session.add(tag_genre)

        media = Media(tag_id=tag.tag_id,
                      media_url=audio_url,
                      media_type="audio")

        db.session.add(media)
    db.session.commit()
Пример #3
0
def create_media(path, media_type):
    """Create and return a new media."""

    media = Media(file_path=path, media_type=media_type)

    db.session.add(media)
    db.session.commit()

    return media
Пример #4
0
def add_media_to_db(tag_id,media_url,media_type):
    """Update database with new media info for tag.
    One tag may have multiple media files"""

    media = Media(tag_id=tag_id,
                  media_url=media_url,
                  media_type=media_type)

    db.session.add(media)
    db.session.commit()
Пример #5
0
def create_media(audition_id, user_id, media_title, link):
    """Creates and returns media"""
    media = Media(audition_id=audition_id,
                  user_id=user_id,
                  media_title=media_title,
                  link=link)

    db.session.add(media)
    db.session.commit()

    return media
Пример #6
0
def upload():
    """Handle uploading new images."""

    filename = images.save(request.files['imageFile'])

    project_id = request.form.get('projectId')

    media = Media('Untitled', images.path(filename))
    db.session.add(media)
    db.session.commit()

    project_media = ProjectMedia(project_id, media.id)
    db.session.add(project_media)
    db.session.commit()

    return get_media_json(media.id)
Пример #7
0
    def post(self):

        if not self.is_login:
            self.redirect(users.create_login_url(self.request.uri))
        filename = self.param('filename')
        do_comment = self.paramint('c', 0)
        if filename[:4] == 'img/':  #处理图片
            new_filename = filename.split('/')[1]
            mtype = new_filename.split('.')[1]
            bits = self.request.body
            media = Media.all().filter('name =', new_filename)
            if media.count() > 0:
                media = media[0]
            else:
                media = Media()
            media.name = new_filename
            media.mtype = mtype
            media.bits = bits
            media.put()
            bid = '_'.join(new_filename.split('_')[:-1])
            entries = Entry.all().filter('slug =', bid)
            if entries.count() > 0:
                entry = entries[0]
                entry.content = entry.content.replace(
                    filename, '/media/' + str(media.key()))
                entry.put()
            return

        if filename == "index.html" or filename[-5:] != '.html':
            return
        #处理html页面
        bid = filename[:-5]
        try:

            soup = BeautifulSoup(self.request.body)
            bp = soup.find(id='bp')
            title = self.getChineseStr(soup.title.text)
            logging.info(bid)
            pubdate = self.getdate(bp.find(id='bp-' + bid + '-publish').text)
            body = bp.find('div', 'blogpost')

            entries = Entry.all().filter('title = ', title)
            if entries.count() < 1:
                entry = Entry()
            else:
                entry = entries[0]


##			entry=Entry.get_by_key_name(bid)
##			if not entry:
##				entry=Entry(key_name=bid)
            entry.slug = bid
            entry.title = title
            entry.author_name = self.login_user.nickname()
            entry.date = pubdate
            entry.settags("")
            entry.content = unicode(body)
            entry.author = self.login_user

            entry.save(True)
            if do_comment > 0:
                comments = soup.find('div', 'comments', 'div')
                if comments:
                    for comment in comments.contents:
                        name, date = comment.h5.text.split(' - ')
                        # modify by lastmind4
                        name_date_pair = comment.h5.text
                        if name_date_pair.index('- ') == 0:
                            name_date_pair = 'Anonymous ' + name_date_pair
                        name, date = name_date_pair.split(' - ')

                        key_id = comment.h5['id']
                        date = self.getdate(date)
                        content = comment.contents[1].text
                        comment = Comment.get_or_insert(key_id,
                                                        content=content)
                        comment.entry = entry
                        comment.date = date
                        comment.author = name
                        comment.save()

        except Exception, e:
            logging.info("import error: %s" % e.message)
Пример #8
0
def create_data():
    """Creating the test data."""

    # If this is run more than once, it will empty out existing data in these tables
    User.query.delete()
    Media.query.delete()

    # Generate test data
    carrot = User(username='******',
                  password=bcrypt.generate_password_hash('bugsbunny'),
                  info='I am a carrot',
                  email='*****@*****.**',
                  background_url='/static/themes/parchment.png',
                  folder_url='static/Testing/testfiles/Carrot')
    potato = User(username='******',
                  password=bcrypt.generate_password_hash('frenchfries'),
                  info='Hi',
                  email='*****@*****.**',
                  background_url='/static/themes/parchment.png',
                  folder_url='static/Testing/testfiles/Potato')
    tomato = User(username='******',
                  password=bcrypt.generate_password_hash('ketchup000'),
                  info='Cheers!',
                  email='tomato@domain',
                  background_url='/static/themes/parchment.png',
                  folder_url='static/Testing/testfiles/Tomato')
    ext_obj = MediaType.query.filter_by(media_ext='jpg').one()
    smiley = Media(media_name='smiley',
                   meta_info='I smile a lot',
                   media_url='static/Testing/testfiles/Carrot/smiley',
                   is_downloadable=True,
                   date_created=datetime.today(),
                   type_of=ext_obj,
                   user=carrot,
                   order=0)
    crossed = Media(media_name='crossed-blob',
                    meta_info='No comment',
                    media_url='static/Testing/testfiles/Potato/crossed-blob',
                    is_downloadable=False,
                    date_created=datetime.today(),
                    type_of=ext_obj,
                    user=potato,
                    order=0)
    curious = Media(media_name='curious-blob',
                    meta_info='HMMM',
                    media_url='static/Testing/testfiles/Potato/curious-blob',
                    is_downloadable=False,
                    date_created=datetime.today(),
                    type_of=ext_obj,
                    user=potato,
                    order=1)
    frown = Media(media_name='frowning-blob',
                  meta_info='',
                  media_url='static/Testing/testfiles/Potato/frowning-blob',
                  is_downloadable=False,
                  date_created=datetime.today(),
                  type_of=ext_obj,
                  user=potato,
                  order=2)
    hand = Media(media_name='Hand',
                 meta_info='Hi',
                 media_url='static/Testing/testfiles/Potato/Hand',
                 is_downloadable=False,
                 date_created=datetime.today(),
                 type_of=ext_obj,
                 user=tomato,
                 order=0)
    wink = Media(media_name='Winky-blob',
                 meta_info='wink',
                 media_url='static/Testing/testfiles/Potato/Winky-blob',
                 is_downloadable=False,
                 date_created=datetime.today(),
                 type_of=ext_obj,
                 user=tomato,
                 order=1)

    db.session.add_all([carrot, potato, tomato])
    db.session.commit()
Пример #9
0
def upload_action():
    # The user
    user = User.query.filter_by(user_id=session['user']).first()
    # Get data
    name = request.form.get('name')
    # Format name for urls
    name = "-".join(name.split(' '))

    # Make sure the name is unique
    media = Media.query.filter_by(media_name=name).first()
    if media:
        flash('You already have art with that name!')
        return redirect('/upload')

    # To handle media file upload - each media will have its own folder in order
    # to hold any thumbnails/textures it has
    # Boolean for whether the file has an associated mtl
    has_mtl = False
    # Check the type of file first
    type_of_file = request.form.get('type')
    # Make a new directory for media
    new_dir_path = os.path.join(user.folder_url, name)
    os.mkdir(new_dir_path)
    if type_of_file == '2D':
        file = request.files['twoD-media']  # Gets the object from form
        extension = (file.filename).rsplit('.', 1)[1].lower()
        if extension not in {'jpg', 'jpeg', 'png', 'webp', 'gif'}:
            flash('Not a valid file. Please follow the accepted file types.')
            return redirect('/upload')
        # The media name is unique for the user (using the formatted name)
        filename = name + '.' + extension
        # Save the file in the user's directory
        file_url = os.path.join(new_dir_path, filename)
        file.save(file_url)
    elif type_of_file == 'OBJ':
        file = request.files['obj-media']
        extension = (file.filename).rsplit('.', 1)[1].lower()
        if extension != 'obj':
            flash('Not a valid file. Please follow the accepted file types.')
            return redirect('/upload')
        filename = name + '.' + extension
        file_url = os.path.join(new_dir_path, filename)
        file.save(file_url)
        mtl_file = request.files['obj-mtl']
        if mtl_file:
            mtl_ext = (mtl_file.filename).rsplit('.', 1)[1].lower()
            if mtl_ext != 'mtl':
                flash(f"Not a valid file. Please follow the accepted " \
                      f"file types.")
                return redirect('/upload')
            mtl_url = os.path.join(new_dir_path,
                                   (mtl_file.filename.rsplit('/', 1)[-1]))
            mtl_file.save(mtl_url)
            has_mtl = True
            for texture in request.files.getlist('obj-textures'):
                text_ext = (texture.filename).rsplit('.', 1)[1].lower()
                if text_ext not in {'jpg', 'jpeg', 'png'}:
                    flash(f"Not a valid file. Please follow the accepted " \
                          f"file types.")
                    return redirect('/upload')
                #save each texture
                texture.save(
                    os.path.join(new_dir_path,
                                 (texture.filename.rsplit('/', 1)[-1])))
    else:  # if type_of_file == 'GLTF'
        file = request.files['gltf-media']
        extension = (file.filename).rsplit('.', 1)[1].lower()
        if extension != 'gltf':
            flash('Not a valid file. Please follow the accepted file types.')
            return redirect('/upload')
        filename = name + '.' + extension
        gltf_bin = request.files['gltf-bin']
        bin_ext = (gltf_bin.filename).rsplit('.', 1)[1].lower()
        if bin_ext != 'bin':
            flash('Not a valid file. Please follow the accepted file types.')
            return redirect('/upload')
        file_url = os.path.join(new_dir_path, filename)
        file.save(file_url)
        gltf_bin.save(
            os.path.join(new_dir_path, (gltf_bin.filename.rsplit('/', 1)[-1])))

    # Rest of form data
    info = request.form.get('metadata')
    downloadable = request.form.get('downloadable') == 'true'
    date = request.form.get('creation')  #YYYY-MM-DD
    tags = request.form.get('tags')
    thumbnail = request.files['thumbnail']

    # Formatting
    tag_list = tags.split('\n')
    # Setting date if no value, else transforming to correct DateTime
    if date:
        date = datetime.strptime(date, "%Y-%m-%d")
    else:
        date = datetime.today()

    # Handling if no thumbnail input
    if thumbnail:
        thumb_ext = (thumbnail.filename).rsplit('.', 1)[1].lower()
        if thumb_ext not in {'jpg', 'jpeg', 'png'}:
            flash(f"Not a valid file. Please follow the accepted " \
                  f"file types.")
            return redirect('/upload')
        thumbnail_url = os.path.join(new_dir_path,
                                     secure_filename(thumbnail.filename))
        thumbnail.save(thumbnail_url)
    else:
        thumbnail_url = None

    # Add media to database
    ext_obj = MediaType.query.filter_by(media_ext=extension).one()
    new_media = Media(media_name=name,
                      meta_info=info,
                      media_url=('/' + file_url),
                      is_downloadable=downloadable,
                      date_created=date,
                      type_of=ext_obj,
                      user=user,
                      order=len(user.owned_media),
                      thumb_url=thumbnail_url)
    if has_mtl:
        new_mtl = ObjToMTL(media=new_media, mtl_url=('/' + mtl_url))
    for tag in tag_list:
        if tag != '':
            tag_existing = Tag.query.filter_by(tag_name=tag.strip()).first()
            if not tag_existing:
                new_media.tags.append(Tag(tag_name=tag.strip()))
            else:
                new_media.tags.append(tag_existing)

    db.session.add(new_media)
    db.session.commit()

    return redirect('/')