# Path: image_filer/utils/files.py
# def generic_handle_file(file, original_filename):
#     """
#     Handels a file, regardless if a package or a single file and returns 
#     a list of files. can recursively unpack packages.
#     """
#     #print "entering generic_handle_file(file=%s, original_filename=%s)" % (file, original_filename)
#     files = []
#     filetype = os.path.splitext(original_filename)[1].lower()
#     #print filetype
#     if filetype=='.zip':
#         unpacked_files = unzip(file)
#         for ufile, ufilename in unpacked_files:
#             files += generic_handle_file(ufile, ufilename)
#     else:
#         files.append( (file,original_filename) )
#     #print "result of generic_handle_file: ", files
#     return files


import os
import filters
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseForbidden, HttpResponseBadRequest
from django.contrib.sessions.models import Session
from django.conf import settings
from django.db.models import Q
from django.core.exceptions import PermissionDenied
from models import Folder, Image, Clipboard, ClipboardItem
from models import tools
from models import FolderRoot, UnfiledImages, ImagesWithMissingData
from django.contrib.auth.models import User
from django import forms
from django.contrib import admin
from image_filer.utils.files import generic_handle_file
                    }, context_instance=RequestContext(request))

def ajax_upload(request, folder_id=None):
    """
    receives an upload from the flash uploader and fixes the session
    because of the missing cookie. Receives only one file at the time, 
    althow it may be a zip file, that will be unpacked.
    """
    #print request.POST
    # flashcookie-hack (flash does not submit the cookie, so we send the
    # django sessionid over regular post
    try:
        engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
        #session_key = request.POST.get('jsessionid')
        session_key = request.POST.get('jsessionid')
        request.session = engine.SessionStore(session_key)
        request.user = User.objects.get(id=request.session['_auth_user_id'])
        #print request.session['_auth_user_id']
        #print session_key
        #print engine
        #print request.user
        #print request.session
        # upload and save the file
        if not request.method == 'POST':
            return HttpResponse("must be POST")
        original_filename = request.POST.get('Filename')
        file = request.FILES.get('Filedata')
        #print request.FILES
        #print original_filename, file
        clipboard, was_clipboard_created = Clipboard.objects.get_or_create(user=request.user)

# Path: image_filer/utils/files.py
# def generic_handle_file(file, original_filename):
#     """
#     Handels a file, regardless if a package or a single file and returns 
#     a list of files. can recursively unpack packages.
#     """
#     #print "entering generic_handle_file(file=%s, original_filename=%s)" % (file, original_filename)
#     files = []
#     filetype = os.path.splitext(original_filename)[1].lower()
#     #print filetype
#     if filetype=='.zip':
#         unpacked_files = unzip(file)
#         for ufile, ufilename in unpacked_files:
#             files += generic_handle_file(ufile, ufilename)
#     else:
#         files.append( (file,original_filename) )
#     #print "result of generic_handle_file: ", files
#     return files


# Path: image_filer/views.py
import os
import filters
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseForbidden, HttpResponseBadRequest
from django.contrib.sessions.models import Session
from django.conf import settings
from django.db.models import Q
from django.core.exceptions import PermissionDenied
from models import Folder, Image, Clipboard, ClipboardItem
from models import tools
from models import FolderRoot, UnfiledImages, ImagesWithMissingData
from django.contrib.auth.models import User
from django import forms
from django.contrib import admin
from image_filer.utils.files import generic_handle_file

                    }, context_instance=RequestContext(request))

def ajax_upload(request, folder_id=None):
    """
    receives an upload from the flash uploader and fixes the session
    because of the missing cookie. Receives only one file at the time, 
    althow it may be a zip file, that will be unpacked.
    """
    #print request.POST
    # flashcookie-hack (flash does not submit the cookie, so we send the
    # django sessionid over regular post
    try:
        engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
        #session_key = request.POST.get('jsessionid')
        session_key = request.POST.get('jsessionid')
        request.session = engine.SessionStore(session_key)
        request.user = User.objects.get(id=request.session['_auth_user_id'])
        #print request.session['_auth_user_id']
        #print session_key
        #print engine
        #print request.user
        #print request.session
        # upload and save the file
        if not request.method == 'POST':
            return HttpResponse("must be POST")
        original_filename = request.POST.get('Filename')
        file = request.FILES.get('Filedata')
        #print request.FILES
        #print original_filename, file
        clipboard, was_clipboard_created = Clipboard.objects.get_or_create(user=request.user)

        files = generic_handle_file(file, original_filename)