def upload_aip(dip, f): dip_name_dir = safe_path_string(dip.name) dip_name_work_dir = os.path.join(config_path_work, dip_name_dir) print "Upload file '%s' to working directory: %s" % (f, dip_name_work_dir) if not os.path.exists(dip_name_work_dir): mkdir_p(dip_name_work_dir) destination_file = os.path.join(dip_name_work_dir, f.name) with open(destination_file, 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) identifier = f.name.rpartition('.')[0] aip = AIP.objects.filter(identifier=identifier) incl = Inclusion.objects.filter(aip=aip, dip=dip) if aip.count() == 0 or incl.count() == 0: aip = AIP.objects.create(identifier=identifier, cleanid="unused", source=destination_file, date_selected=timezone.now()) Inclusion(aip=aip, dip=dip, stored=True).save() else: print "Object not added, already exists"
def add_representation(request, pk): data = {"success": False} ip = InformationPackage.objects.get(pk=pk) representation = request.POST['representation'] try: if re.match("[A-Za-z0-9-_]{4,200}", representation, flags=0): repr_dir = os.path.join(ip.path, "representations", representation) if not os.path.exists(repr_dir): mkdir_p(repr_dir) mkdir_p(os.path.join(repr_dir, 'metadata')) mkdir_p(os.path.join(repr_dir, 'schemas')) mkdir_p(os.path.join(repr_dir, 'data')) mkdir_p(os.path.join(repr_dir, 'documentation')) print representation request.session['rep'] = representation data = {"success": True, "representation": representation} else: data = { "success": False, "message": "Representation already exists!" } else: data = { "success": False, "message": "Invalid representation directory name (alphanumerical with minimum length 4 and maximum length 200)!" } except: tb = traceback.format_exc() return JsonResponse(data)
def initialize(request, packagename): uuid = getUniqueID() sip_struct_work_dir = os.path.join(config_path_work, uuid) mkdir_p(os.path.join(sip_struct_work_dir, 'metadata/descriptive')) #mkdir_p(os.path.join(sip_struct_work_dir, 'schemas')) mkdir_p(os.path.join(sip_struct_work_dir, 'representations')) #copy_tree_content(os.path.join(root_dir, "earkresources/schemas"), os.path.join(sip_struct_work_dir, 'representations/rep-001/schemas')) ignore_patterns = ('*.pyc', '*.git', 'tmp') shutil.copytree(os.path.join(root_dir, "earkresources/schemas"), os.path.join(sip_struct_work_dir, 'schemas'), ignore=shutil.ignore_patterns(*ignore_patterns)) #shutil.copyfile(os.path.join(root_dir, "earkresources/schemas/IP.xsd"), os.path.join(sip_struct_work_dir, 'schemas/IP.xsd')) wf = WorkflowModules.objects.get(identifier=SIPReset.__name__) InformationPackage.objects.create(path=os.path.join( config_path_work, uuid), uuid=uuid, statusprocess=0, packagename=packagename, last_task=wf) ip = InformationPackage.objects.get(uuid=uuid) ip_state_xml = IpState.from_parameters(state=0, locked_val=False, last_task_value=SIPReset.__name__) ip_state_xml.write_doc(os.path.join(config_path_work, uuid, "state.xml")) return HttpResponse(str(ip.id))
def upload_file(upload_path, f): print "Upload file '%s' to working directory: %s" % (f.name, upload_path) if not os.path.exists(upload_path): mkdir_p(upload_path) destination_file = os.path.join(upload_path, f.name) with open(destination_file, 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) destination.close()
def upload_aip(ip_work_dir, upload_path, f): print "Upload file '%s' to working directory: %s" % (f, upload_path) if not os.path.exists(upload_path): mkdir_p(upload_path) destination_file = os.path.join(upload_path, f.name) with open(destination_file, 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) destination.close() if f.name.endswith(".tar"): async_res = extract_and_remove_package.delay( destination_file, upload_path, os.path.join(ip_work_dir, 'metadata/sip_creation.log')) print "Package extraction task '%s' to extract package '%s' to working directory: %s" % ( async_res.id, f.name, upload_path)
def add_file(request, uuid, subfolder): ip = InformationPackage.objects.get(uuid=uuid) # template = loader.get_template('sipcreator/detail.html') # upload_file_form = TinyUploadFileForm() # repr_dir = os.path.join(ip.path,"representations") repname = "" if request.POST.has_key('rep'): repname = request.POST['rep'] repsubdir = "" if request.POST.has_key('subdir'): repsubdir = request.POST['subdir'] # context = RequestContext(request, { # 'uuid': ip.uuid, # 'StatusProcess_CHOICES': dict(StatusProcess_CHOICES), # 'config_path_work': config_path_work, # 'uploadFileForm': upload_file_form, # 'repr_dirs': filter(lambda x: os.path.isdir(os.path.join(repr_dir, x)), os.listdir(repr_dir)), # 'ip': ip, # 'rep': rep, # }) if subfolder.startswith("_root_"): subfolder = subfolder.replace("_root_", ".") ip_work_dir = os.path.join(config_path_work, uuid) upload_path = os.path.join(ip_work_dir, subfolder, repname, repsubdir) print upload_path if not os.path.exists(upload_path): mkdir_p(upload_path) if request.method == 'POST': form = TinyUploadFileForm(request.POST, request.FILES) if form.is_valid(): upload_aip(ip_work_dir, upload_path, request.FILES['content_file']) else: if form.errors: for error in form.errors: print(str(error) + str(form.errors[error])) if not repname and request.session.has_key('rep'): repname = request.session['rep'] url = "/earkweb/sipcreator/detail/%s/%s/" % (str(ip.id), repname) return HttpResponseRedirect(url)
def ins_file(request, uuid, subfolder): ip = InformationPackage.objects.get(uuid=uuid) repname = "" if request.POST.has_key('rep'): repname = request.POST['rep'] print "repname=%s" % repname repsubdir = "" if request.POST.has_key('subdir'): repsubdir = request.POST['subdir'] print "repsubdir=%s" % repsubdir if subfolder.startswith("_root_"): subfolder = subfolder.replace("_root_", ".") print "subfolder=%s" % subfolder ip_work_dir = os.path.join(config_path_work, uuid) upload_path = os.path.join(ip_work_dir, subfolder, repname, repsubdir) print "upload_path=%s" % upload_path if not os.path.exists(upload_path): mkdir_p(upload_path) if request.method == 'POST': form = TinyUploadFileForm(request.POST, request.FILES) print request.FILES if form.is_valid(): print "valid form" upload_aip(ip_work_dir, upload_path, request.FILES['content_file']) else: if form.errors: for error in form.errors: print(str(error) + str(form.errors[error])) return HttpResponse("success")