示例#1
0
文件: views.py 项目: maralad/shabingo
def upload2(request):
        # Handle file upload
    logger = logging.getLogger(__name__)
    
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
                                  
        if form.is_valid():
            
            newdoc = Document(docfile = request.FILES['docfile'], docfile1 = request.FILES['docfile1'],  docfile2 = request.FILES['docfile2'])
            newdoc.usersname = request.user.username
            newdoc.price=request.POST.get('price','')
            newdoc.category= request.POST.get('category','')
            newdoc.isover18s =request.POST.get('isover18s','')
            newdoc.name =request.POST.get('name','')
            newdoc.description =request.POST.get('description','')
   
            
            form.clean_content()
            
               
            newdoc.save()

            return HttpResponseRedirect(reverse('main.views.upload'))
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()

    # Render list page with the documents and the form
    return render_to_response(
        'upload2.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request)
    )
示例#2
0
文件: views.py 项目: maralad/shabingo
def upload(request):
    # Handle Video upload

    # A lot going on here especially shell commands
    # 3 files get uploaded with various meta info fields
    # Using FFMPEG 2 videos are converted to mp4 regardless of 
    # what they are when uploaded first. Then they get renamed, 
    # saved and the original ones are deleted
    # A poster image for the first frame is also uploaded and 
    # resized using image Magick.
    # Function execute_shell is used a lot sending lots of commands through
    # pythons subprocess synchronously
    # May redo this so it is done asynchronously but afraid people will think
    # Nothing happened and not come back. 
    
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
                                  
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'], docfile1 = request.FILES['docfile1'],  docfile2 = request.FILES['docfile2'])
            upload_file=request.FILES['docfile']
            upload_file1=request.FILES['docfile1']
            upload_file2=request.FILES['docfile2']
           
            #logger.debug('*************************************New Filename is {0}************************************'.format(newdoc.docfile.name))
            newdoc.usersname = request.user.username
            newdoc.price=request.POST.get('price','')
            newdoc.category= request.POST.get('category','')
            newdoc.isover18s =request.POST.get('isover18s','')
            newdoc.name =request.POST.get('name','')
            newdoc.description =request.POST.get('description','')
            bla=request.POST.get('document_type','')
            logger.debug('bla bla bla ={0}'.format(bla))
            if bla=='on':
                newdoc.document_type =1
            else:
                newdoc.document_type =0
            #Temporarily commented out. lots of error handling on front end
            #form.clean_content()
            newdoc.save()
            logger.debug('File name = {0}'.format(newdoc.name))
            
            new_file=get_new_file(newdoc.docfile.name)            
          
            new_file1=get_new_file(newdoc.docfile1.name)

            #New folders and need to be created
            today_folder =datetime.date.today().strftime("%Y/%m/%d")

            
            str_= str(newdoc.docfile2.name)
            str_file2="_thumb_" +str_.split('/')[-1]
            #create destination of converted files
            prefix='/home/donagh/webapps/shabingo_static/media/'
            output=os.path.join('MEDIA_ROOT','documents',today_folder,new_file)
            output1=os.path.join('MEDIA_ROOT','documents',today_folder,new_file1) 
            output2=os.path.join('MEDIA_ROOT','documents',today_folder,str_file2)
            
            output_file=prefix+output
            output_file1=prefix+output1
            output_file2=prefix+output2
            
            str_file=str(newdoc.docfile.name)
            str_file=prefix+str_file
            str_file1=str(newdoc.docfile1.name)
            str_file1=prefix+str_file1
            str_file2=str(newdoc.docfile2.name)
            str_file2=prefix+str_file2
            
            #The magic of FFMPEG ...  Convert  video to mp4      
            shell_cmd='ffmpeg -i '+str_file+' -vcodec libx264 -acodec libfaac '+ output_file
            #logger.debug('shell_cmd ...{0}'.format(shell_cmd))
            file_details=''
            
            try:
                file_details=execute_shell(shell_cmd)
                newdoc.rename_shab(output)
            except Exception as e:
                logger.debug('Failed to execute shell command ...{0}'.format(e))
            
            #The magic of FFMPEG ...  Convert  video to mp4   
            shell_cmd1='ffmpeg -i '+str_file1+' -vcodec libx264 -acodec libfaac '+ output_file1
          
            file_details=''
            
            
            try:
                file_details=execute_shell(shell_cmd1)
                newdoc.rename_prev(output1)
            except Exception as e:
                logger.debug('Failed to execute shell command ...{0}'.format(e))            
            
            #Resize Poster image uploadeed to keep it all perfect size and limit disk space
            shell_cmd2 ="""convert %s -resize 300x200\! %s
            """%(str_file2,str_file2)
            #logger.debug(' THUM NAIL CONVERSION COMMAND === {0}'.format(shell_cmd2))
            
            try:
                file_details=execute_shell(shell_cmd2)
                #newdoc.rename_thumb(output2)
            except Exception as e:
                logger.debug('Failed to execute shell command ImageMagick with this error...{0}'.format(e))            

            #Remove original uploaded files so we minimize disk storage
            shell_rm_file = "rm %s" %str_file
            shell_rm_file1 = "rm %s" %str_file1
            
            try:
                file_details=execute_shell(shell_rm_file)
                file_details=execute_shell(shell_rm_file1)

            except Exception as e:
                logger.debug('Problem remo ing originally uploaded files with this error: {0}'.format(e))            

            subject = 'ATTENTION: Shabingo Video Upload Approval required.'
            msg='User %s as uploaded video: %s plesae review at : %s and also %s and %s'%(newdoc.usersname,newdoc.name, newdoc.docfile.url, newdoc.docfile1.url, newdoc.docfile2.url)
            message = msg
            sender = ['*****@*****.**']
            cc_myself = ['*****@*****.**']
            recipients = ['*****@*****.**','*****@*****.**','*****@*****.**']
            #recipients.append(sender)
            send_mail(subject, message, '*****@*****.**', recipients)

            return HttpResponseRedirect(reverse('main.views.upload'))
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    #documents = Document.objects.all() # This is madness especially when the amount of videos uploaded grows
    documents = Document.objects.filter(usersname=request.user.username)
    # Render list page with the documents and the form
    return render_to_response(
        'upload.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request)
    )