upload files hacked together to work...

This commit is contained in:
Sanj 2011-06-01 06:40:55 +05:30
parent bdf16adb52
commit 6fba074d8f
5 changed files with 156 additions and 14 deletions

View File

@ -47,6 +47,26 @@ def ignoreFile(f):
#This function is called from the post_save signal, receives kwargs['instance'] as a File instance
def convertFile(**kwargs):
f = kwargs['instance']
if not f.done:
return
if f.type == '':
if f.ext == '':
fname = str(f.file)
ext = e.models.extFileName(fname)
f.ext = ext
if f.ext in ['jpg', 'JPG']:
f.type = 'image'
elif f.ext in ['ogg', 'oga']:
f.type = 'audio'
elif f.ext in ['ogv']:
f.type = 'video'
else:
f.type = 'other'
f.save()
if str(f.file).startswith(MEDIA_ROOT):
f.file = str(f.file).replace(MEDIA_ROOT, "")[1:]
f.save()
fn = {
'image': ignoreFile,
'audio': toOgg,
@ -55,7 +75,6 @@ def convertFile(**kwargs):
'other': ignoreFile,
# 'other': ignoreFile
}
f = kwargs['instance']
fn[f.type](f)
return

View File

@ -92,7 +92,7 @@ class File(models.Model):
if not self.done:
if not self.file:
self.file.save(name, ContentFile(chunk))
self.filename = name
self.title = name
self.save()
else:
f = open(self.file.path, 'a')
@ -174,4 +174,4 @@ class Type(models.Model):
verbose_name = 'File Type'
verbose_name_plural = 'File Types'
# post_save.connect(convertFile, sender=File)
post_save.connect(convertFile, sender=File)

View File

@ -4,8 +4,9 @@ import views
urlpatterns = patterns('',
(r'addFolder', views.add_folder),
(r'upload', views.upload_files),
(r'add_category', views.add_category),
(r'add', views.add),
(r'^([A-Z0-9].*)/chunk$', views.chunk)
(r'^([A-Z0-9].*)/chunk$', views.chunk),
)
# urlpatterns += patterns('views',

View File

@ -26,8 +26,26 @@ def getFolderList():
return map(lambda x: (basename(x), basename(x)), full_dirs)
@login_required
def upload_files(request):
return render_to_response("upload_files.html")
def upload_files(request):
return render_to_response("upload_files.html", {
'categoryForm': CategoryForm()
})
@login_required
def add_category(request):
category_id = request.GET['category_id']
try:
c = Category.objects.get(pk=category_id)
except:
category_name = request.GET['category_name']
if category_name != '':
try:
c = Category.objects.get(name=category_name)
except:
c = Category()
c.name = category_name
c.save()
return render_to_json_response({'category_id': c.id})
class FolderSelect(forms.widgets.Select):
def _get_choices(self):
@ -54,6 +72,10 @@ class AddFolderForm(forms.Form):
category = forms.ModelChoiceField(Category.objects, required=False, label="Study")
category_name = forms.CharField(required=False, label="Create New Study")
class CategoryForm(forms.Form):
category = forms.ModelChoiceField(Category.objects, required=False, label="Study")
category_name = forms.CharField(required=False, label="Create New Study")
@login_required
def add_folder(request):
if request.POST:
@ -107,7 +129,8 @@ def chunk(request, id):
if form.is_valid() and canEdit:
f = form.cleaned_data['chunk']
response = {
'resultUrl': '/files/' + str(item.id)
'resultUrl': '/files/' + str(item.id),
'fileId': item.id
}
if item.title:
name = item.title
@ -138,6 +161,10 @@ def add(request):
file.info = '' #WTF!
file.title = request.POST.get('name', '')
file.save()
category_id = request.POST.get('category', 1)
c = Category.objects.get(pk=category_id)
file.categories.add(c)
file.save()
response = {
'result': 1,
'maxRetry': 10,

View File

@ -257,8 +257,9 @@ $(document).ready(function() {
if(Chrome || WebKitVersion >= '534.28' ||
(GeckoVersion >= '2.0')) {
$('#firefogg').hide();
$('#submit').hide();
// $('#firefogg').hide();
// $('#submit').hide();
/*
$('#file').change(function(e) {
if(this.files.length>0) {
$('#file').hide();
@ -271,13 +272,16 @@ $(document).ready(function() {
} else {
$('#upload').hide();
}
*/
/*
$('#upload').click(function(e) {
$('#upload').hide();
e.stopPropagation();
var file = $('#file')[0].files[0];
var data = {
'firefogg': 1,
'name': file.name // Send filename here since Blobs will have no name later
'name': file.name, // Send filename here since Blobs will have no name later
'category': $('#files_category').val()
};
var ogg = FirefoggUploader(file, add_url, data, function(ogg) { //callback
if(ogg.resultUrl) {
@ -292,10 +296,101 @@ $(document).ready(function() {
});
$('#submitFile').hide();
$('#progressbar').show();
$('#progressbar').width(200);
$('#progressbar').css('background-color', '#eee');
$('#progressbar').html('<div id="progress" style="background-color: #666;height:20px;width:0%" /><div id="progressstatus" style="background-color: #fff;">uploading</div>');
});
*/
}
});
function doProgress(progress) {
//do something with status and progress, i.e. set progressbar width:
$('#progress').css('width', parseInt(progress*100, 10) +'%');
$('#progressstatus').html(parseInt(progress*100, 10) + '% - ' + ogg.status);
}
var UploadQueue = function() {
var that = this;
this.allFiles = [];
this.isUploading = false;
this.$list = $('#filesList');
this.len = function() {
return this.allFiles.length;
};
this.getLi = function(no) {
return this.$list.children('li').eq(no);
}
this.init();
}
UploadQueue.prototype.init = function() {
$('#progressbar').show();
$('#progressbar').width(200);
$('#progressbar').css('background-color', '#eee');
$('#progressbar').html('<div id="progress" style="background-color: #666;height:20px;width:0%" /><div id="progressstatus" style="background-color: #fff;">uploading</div>');
}
/* param f = file */
UploadQueue.prototype.getData = function(f) {
return {
'firefogg': 1,
'name': f.name,
'category': $('#files_category').val()
};
};
UploadQueue.prototype.uploadNext = function() {
};
UploadQueue.prototype.addFile = function(f) {
for (var i=0; i<this.len(); i++) {
if (this.allFiles[i].name == f.name) {
return false;
}
}
this.allFiles.push(f);
this.addFileToList(f);
// this.$list.triggerEvent("fileAdded");
if (!this.isUploading) {
this.upload(this.len() - 1);
}
return this;
};
UploadQueue.prototype.addFileToList = function(f) {
var that = this;
var $li = $('<li />').data("file", f).text(f.name).appendTo(that.$list);
};
UploadQueue.prototype.markDone = function(no) {
$('.uploading').removeClass("uploading");
this.getLi(no).addClass("uploaded");
if (this.len() > (no + 1)) {
this.upload(no + 1);
} else {
this.isUploading = false;
}
};
UploadQueue.prototype.upload = function(no) {
var that = this;
var fil = this.allFiles[no];
console.log("uploading", fil);
var data = this.getData(fil);
var $li = this.getLi(no);
this.isUploading = true;
$li.addClass("uploading");
ogg = FirefoggUploader(fil, add_url, data, function(ogg) {
if (ogg.resultUrl) {
that.markDone(no);
} else {
$('#progressbar').html(ogg.status);
}
}, doProgress);
};