def downloadfile(req, id): model = HashID.get_related(id, 'hpshare_storage') if not model.uploaded: raise Http404("File {} is not ready".format(id)) if req.method == 'GET': model.download_count = F('download_count') + 1 model.save() return HttpResponseRedirect(gen_qn_url(model.key_name, True))
def newgroup(req): form = NewgroupForm(req.POST) if not form.is_valid(): return HttpResponseBadRequest() ids = form.cleaned_data['ids'].split(',') if not ids: return HttpResponseBadRequest() model = StorageGroup() model.hashid = HashID.new(private=form.cleaned_data['private']) model.save() for id in ids: model.storages.add(HashID.get_related(id, 'hpshare_storage')) return JsonResponse({ 'url': req.build_absolute_uri(reverse('hpshare_viewgroup', args=[model.hashid.hashid,])), 'count': len(ids), })
def viewgroup(req, id): model = HashID.get_related(id, 'hpshare_storage_group') storages = list(model.storages.all()) if len(storages) == 0: raise Http404("Group {} has no storage.".format(id)) return render(req, 'viewgroup.html', { 'storages': storages, 'model': model, })
def modify(req, id): form = ShortenForm(req.POST) if not form.is_valid(): return HttpResponseBadRequest() model = HashID.get_related(id, 'hpshorten_redirect', inc=False) for key in ('url', 'permanent', 'cloak', 'title'): setattr(model, key, form.cleaned_data[key]) model.save() return JsonResponse({ "url": req.build_absolute_uri(reverse('hpshorten_redirect', args=[model.hashid.hashid, ])), "target": model.url })
def create(req): form = ShortenForm(req.POST) if not form.is_valid(): return HttpResponseBadRequest() model = Redirection() model.hashid = HashID.new(private=form.cleaned_data['private']) for key in ('url', 'permanent', 'cloak', 'title'): setattr(model, key, form.cleaned_data[key]) model.save() return JsonResponse({ "url": req.build_absolute_uri(reverse('hpshorten_redirect', args=[model.hashid.hashid, ])), "target": model.url })
def permit(req): form = PermitForm(req.POST) if not form.is_valid(): return HttpResponseBadRequest() model = Storage() model.hashid = HashID.new(private=form.cleaned_data['private']) model.filename = form.cleaned_data['filename'] model.sha1sum = form.cleaned_data['sha1sum'].strip() model.size = form.cleaned_data['fsize'] model.save() options = { 'insertOnly': 1, 'saveKey': model.key_name, 'fsizeLimit': model.size, 'callbackUrl': req.build_absolute_uri(reverse('hpshare_api:callback')), 'callbackBody': CallbackForm.getCallbackBody(), } if model.sha1sum: options['checksum'] = 'SHA1:{}'.format(model.sha1sum) persistents = get_persistents(req, model) if persistents: saveas = config.BUCKET_NAME + ':' + model.key_name.encode('utf8') ops = map(lambda x: '{}|saveas/{}' .format(x[0], urlsafe_b64encode(saveas + x[1])), persistents) options['persistentOps'] = ';'.join(ops) options['persistentNotifyUrl'] = req.build_absolute_uri(reverse('hpshare_api:persistent_callback')) options['persistentPipeline'] = config.PERSISTENT_PIPELINE token = qn.upload_token(config.BUCKET_NAME, None, config.UPLOAD_TIME_LIMIT, options, False) # strict_policy: false country = geoip.country_code(req.META.get('REMOTE_ADDR', '')) if country == 'CN' or country is None: upload_domain = 'up.qiniu.com' else: upload_domain = 'up.qiniug.com' return JsonResponse({'token': token, 'upload_domain': upload_domain})
def viewfile(req, id, disable_preview=False): model = HashID.get_related(id, 'hpshare_storage') persistents = model.converted_storage.filter(success=True, is_preview=False) preview_template = None preview_model = None if not disable_preview: preview_model = model.converted_storage\ .filter(success=True, is_preview=True)[0:1] try: preview_model = preview_model.get() except ObjectDoesNotExist: preview_model = None preview_template = get_preview_template(preview_model, model) return render( req, 'viewfile.html', { 'model': model, 'preview_model': preview_model, 'preview_template': preview_template, 'extrainfo': filter(len, model.extrainfo.split('\n')), "persistents": persistents, })
def persistent_callback(req): data = json.loads(req.body) try: source = Storage.objects.get(persistentId=data['id']) except Storage.DoesNotExist: return HttpResponseServerError("Not found, try later.") persistents = get_persistents(req, source) def find_suffix_desc(cmd): cmd = cmd.rpartition('|')[0] # strip 'saveas' for p in persistents: if cmd == p[0]: return p[1], p[2], p[3] return ('', '', False) for item in data['items']: model = ConvertedStorage() model.hashid = HashID.new(private=True) model.source = source model.success = (item['code'] == 0) model.error_msg = item.get('error', '') model.key = item.get('key', '') model.cmd = item['cmd'] model.suffix, model.description, model.is_preview = find_suffix_desc(item['cmd']) model.save() return JsonResponse({})
def redirect(req, id): return do_redirect(req, HashID.get_related(id, 'hpshorten_redirect'))