def doShare(path): is_private = False is_public = False try: f = File.get(File.public_share_url == path) is_public = True except peewee.DoesNotExist: try: f = File.get(File.private_share_url == path) is_private = True except peewee.DoesNotExist: return jsonify(message='error'), 404 if not ((is_public and f.open_public_share) or (is_private and f.open_private_share)): return jsonify(message='error'), 404 args = request.args if 'password' in args: if args['password'] == f.private_share_password: return jsonify(message='OK') else: return jsonify(message='error'), 401 s = Serializer(app.config['SECRET_KEY']) token = s.dumps({'path': path}) payload = { 'filename': f.filename, 'folder': f.folder.name, 'openPublic': f.open_public_share, 'openPrivate': f.open_private_share, 'token': token, } return jsonify(message='OK', payload=payload)
def already_downloaded(filepath): ''' Return true if we already have this version of the file (check date and file hash). False otherwise ''' try: File.get(File.sha1 == sha1OfFile(filepath)) return True except peewee.DoesNotExist: return False
def summary(): text = '<h1>Database Summary</h1>\n' file1 = File.get(id=1) file2 = File.get(id=2) file3 = File.get(id=3) text += '<h2>' + file1.name + '</h2>\n' text += '<h2>' + file2.name + '</h2>\n' text += '<h2>' + file3.name + '</h2>\n' return text
def share(path): is_public = False try: f = File.get(File.public_share_url == path) actual_filename = generate_filename(f.folder.name, f.filename) target_file = os.path.join(os.path.expanduser(config.UPLOAD_FOLDER), actual_filename) is_public = True except peewee.DoesNotExist: return jsonify(message='error'), 404 if not (is_public and f.open_public_share): return jsonify(message='error'), 404 s = URLSafeSerializer(config.SECRET_KEY, expires_in=24 * 3600) args = request.args if args.get('download') == 'true': return redirect("/share/download/" + path + "/" + f.filename) share_token = s.dumps({'path': path}).decode('utf-8') payload = { 'filename': f.filename, 'folder': f.folder.name, 'open_public_share': f.open_public_share, 'share_token': share_token, } return jsonify(message='OK', data=payload)
def post(self): item = None vals = {} # get all the incoming values title = self.request.get('title').strip() blob_key = None label_raw = self.request.get('label_raw').strip() # get the file information uploaded_files = self.get_uploads('file') if len(uploaded_files) == 1: blob_info = uploaded_files[0] blob_key = blob_info.key() if self.request.get('key'): item = File.get( self.request.get('key') ) item.title = title item.label_raw = label_raw else: item = File( title = title, label_raw = label_raw, ) if blob_key: item.blob = blob_key # update and save this file item.set_derivatives() item.put() self.redirect('.')
def is_project_file(filename, project_id): """ Checks, if a file belongs to a project. Checks both project folder and database. :param filename: a filepath. :type filename: String :param project_id: Project id from database. :type project_id: Integer :rtype: Boolean """ filebase = os.path.basename(filename) fileabs = os.path.abspath(filename) if File.get('exists', File.path == fileabs, File.project_id == project_id): return True project = Project.get_by_id(project_id) file_project_path = filesystem.search_file(filebase, project.dir) project = None if file_project_path: try: File(file_project_path, project_id) return True except (ItemAlreadyExistsException, SQLAlchemyError): pass return False
def share_download(path, filename): try: f = File.get(File.public_share_url == path) actual_filename = generate_filename(f.folder.name, f.filename) target_file = os.path.join(os.path.expanduser(config.UPLOAD_FOLDER), actual_filename) except peewee.DoesNotExist: return jsonify(message='error'), 404 s = URLSafeSerializer(config.SECRET_KEY, expires_in=24 * 3600) share_token = None cookies = request.cookies if 'share_token' in cookies: share_token = cookies['share_token'] try: data = s.loads(share_token) if data['path'] == path: if os.path.exists(target_file): return send_file(target_file) else: return jsonify(message='error'), 404 else: return jsonify(message='unauthorized'), 401 except: return jsonify(message='unauthorized'), 401
def update_metadata(self): metadata = File.get(md5=self.preview).get_metadata() # Handle Static markup markup = "" markup += "<b>Name:</b> %s<br />" % metadata['name'] markup += "<b>Path:</b> %s<br />" % metadata['path'] markup += "<b>md5:</b> %s<br />" % metadata['md5'] markup += "<b>Width:</b> %s<br />" % metadata['width'] markup += "<b>Height:</b> %s<br />" % metadata['height'] markup += "<b>Type:</b> %s<br />" % metadata['type'] markup += "<b>size:</b> %s<br />" % metadata['size'] # Handle dynamic Markup fields = defaultdict(set) for m in metadata['metadata']: fields[m.field].add(m.value) for field, values in fields.items(): links = [ "<a href='%s:\"%s\"'>%s</a>" % (field, value, value) for value in values ] markup += "<b>%s:</b> %s<br />" % (field, ", ".join(links)) self.DetailsWindow.SetPage(markup)
def ImportJsonOnMenuSelection(self, event): openFileDialog = wx.FileDialog(self, "Open MEtadat file", "", "", "JSON files (*.json)|*.json", wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) if openFileDialog.ShowModal() == wx.ID_CANCEL: return # the user changed idea... # proceed loading the file chosen by the user # this can be done with e.g. wxPython input streams: data = None with open(openFileDialog.GetPath(), 'r') as f: data = json.load(f) for md5, metadata in data.items(): try: f = File.get(File.md5 == md5) except Exception as e: logger.error("Unable to get file for md5:%s", md5) logger.exception(e) continue # Don't do more work with this file for field, values in metadata.items(): if field == 'import-time': return for value in values: try: Metadata(file=f, field=field, value=value).save() except peewee.IntegrityError as e: logger.info("Duplicate metadata ignored (%s, %s, %s)", md5, field, value) except Exception as e: logger.error("Unable to save metadata (%s, %s, %s)", md5, field, value) logger.exception(e) self.update_metadata() self.update_tags()
def ingested(filepath): '''Return true if file is already ingested, false otherwise''' # TODO: implement better (just checks if file in table right now) try: myfile = File.get(name=filepath) print "%s already in database." % filepath return True except: return False
def edit(request, key): file = File.get(key) form = FileEditForm(request.form, obj=file) if request.method == "POST" and form.validate(): form.auto_populate(file) file.put() if form.save.data is True: return redirect(url_for('nut:files/list'), 301) return render_template('app:files/form.html', form=form, file=file)
def get_tags(file_name): """Retreives a list of the tags for a specific file""" if File.get(File.file_name == file_name): file_tags = (Tag .select() .join(FileTag) .where(FileTag.file_id == File.get(File.file_name == file_name))) tag_list = [] for t in file_tags: tag_list.append(t.tag_name) return tag_list else: print "Sorry! Couldn't understand request!"
def get(self): item = None if self.request.get('key'): item = File.get( self.request.get('key') ) vals = { 'item' : item, # this is either new.html or edit.html 'upload_url' : blobstore.create_upload_url( str(urllib.unquote(self.request.path)) ), } self.template( 'file-form.html', vals, 'admin' );
def delete_entry(kind, entry, from_file=None): """Deletes a file name entry or tag entry from the catalog""" if kind == 'files': to_delete = File.get(File.file_name == entry) to_delete.delete_instance() print "'%s' deleted from catalog!" % entry elif kind == 'tag' and from_file: try: existing_file = File.get(File.file_name == from_file) if existing_file: try: association = FileTag.get(FileTag.file_id == existing_file, FileTag.tag_id == Tag.get(Tag.tag_name == entry)) association.delete_instance() print "Deleted '%s' from '%s'" % (entry, from_file) except: print "'%s' no longer tagged to %s" % (entry, from_file) except: print "'%s' not in catalog" % from_file elif kind == 'tag': tag_item = Tag.get(Tag.tag_name == entry) tag_item.delete_instance() print "'%s' deleted from catalog" % entry else: pass
def _on_moved_project(self, event): """ On_modified event handler for project files. This should get called when a project file has been moved. - If the file exists in database: - If the target path is inside project directory, change the\ path entry in database. - If the target path is outside project directory, set the\ file as deleted (project.id = NULL). - If the file does not exist in database: - If the target path is inside the project directory, add the\ file into the database. - If the target path is outside the project directory, do nothing. :param event: The event. :type event: :py:class:`watchdog.events.FileSystemEvent` """ source = os.path.abspath(event.src_path) target = os.path.abspath(event.dest_path) try: project = Project.get_by_id(self.project_id) source_file = File.get('one', File.project == project, File.path == source) project_path = os.path.abspath(project.dir) if source_file is not None: # Case 1 if target.startswith(project_path): source_file.path = target act_path = target action = REVERSE_ACTIONS['Renamed to something'] # Case 2 else: source_file.project_id = None act_path = source action = REVERSE_ACTIONS['Deleted'] source_file.update() self._project_prototype(act_path, action) else: # Case 3 if target.startswith(project_path): action = REVERSE_ACTIONS['Created'] self._project_prototype(target, action) # Case 4, should never happen. else: pass except Exception as excp: log_msg = ('Exception in Project file handler on_moved ' 'callback: {exception!s}') log_msg = log_msg.format(exception=excp) _logger().exception(log_msg)
def delete(self, id): file = File.get(id) if file: if file.user_id != g.current_user.id: abort(401, error='Unauthorized access') try: remove(path.join(Setting.UPLOAD_FOLDER, file.save_name)) except: abort(404, error='File not exist.') file.delete() return '', 204 else: abort(404, error='File not exist.')
def get(self): try: if self.request.get('key'): item = File.get( self.request.get('key') ) vals = { 'item' : item, } self.template( 'file-del.html', vals, 'admin' ); else: self.redirect('.') except: self.redirect('.')
def delete(request, key): form = FileConfirmDeleteForm(request.form) if request.method == "POST" and form.validate(): if form.drop.data is True: File.drop(key) return redirect(url_for('nut:files/list'), 301) if form.cascade.data is True: File.drop(key, cascade=True) return redirect(url_for('nut:files/list'), 301) file = File.get(key) files = dict([(n.get_key(), n) for n in File.all().filter("ancestors = ", key)]) file = rec(file, files) return render_template('app:files/confirm_delete.html', file=file, form=form)
def files(folder_name, filename): actual_filename = secure_filename(folder_name + '_' + filename) try: f = File.get(filename=filename) except peewee.DoesNotExist: return jsonify(message='error'), 404 if request.method == 'GET': args = request.args if 'query' in args and args['query'] == 'info': payload = { 'filename': f.filename, 'public': f.public_share_url, 'private': f.private_share_url, 'password': f.private_share_password, 'openPublic': f.open_public_share, 'openPrivate': f.open_private_share } return jsonify(message='OK', payload=payload) return send_from_directory(app.config['UPLOAD_FOLDER'], actual_filename) if request.method == 'DELETE': actual_filename = secure_filename(folder_name + '_' + filename) file_path = os.path.join(app.config['UPLOAD_FOLDER'], actual_filename) if os.path.exists(file_path): f.delete_instance() os.remove(file_path) return jsonify(message='OK') else: return jsonify(message='error'), 404 if request.method == 'PUT': req = request.get_json() share_type = req['shareType'] if share_type == 'openPrivate': f.open_private_share = True f.open_public_share = False elif share_type == 'openPublic': f.open_private_share = False f.open_public_share = True elif share_type == 'None': f.open_public_share = False f.open_private_share = False f.save() return jsonify(message='OK')
def post(self): logging.getLogger().info("Post is %s" % self.request.POST) fil = self.request.POST["file_2"] if not fil.value: self.redirect("/") return file = File.get(self.request.get("csskey")) file.name = fil.filename file.file = db.Blob(cssmin.cssmin(fil.value)) file.original = db.Blob(fil.value) file.content_type = fil.type file.user = users.get_current_user() file.put() self.redirect("/")
def update_preview(self): if self.preview is None: return # The logic for creating a accurate image as large as can be seen preview = wx.Image() preview.LoadFile(File.get(md5=self.preview).path) width, height = self.PreviewPanel.GetSize() hRatio = height / preview.Height wRatio = width / preview.Width ratio = min(hRatio, wRatio) image = preview.Scale(preview.Width * ratio, preview.Height * ratio, wx.IMAGE_QUALITY_HIGH) result = image.ConvertToBitmap() # set the Result. self.Preview.SetBitmap(result)
def post(self): try: item = File.get( self.request.get('key') ) if self.request.get('key') else None if item is not None: try: if item.blob: item.blob.delete() item.delete() self.redirect('.') except: vals = { 'item' : item, 'err' : 'There was an error when deleting this file, please try again' } self.template( 'file-del.html', vals, 'admin' ); except: self.redirect('.')
def folder(folder_name): try: folder = Folder.get(Folder.name == folder_name) except peewee.DoesNotExist: return jsonify(message="error"), 404 if request.method == "GET": return jsonify(message="OK", data=model_to_dict(folder, backrefs=True)) if request.method == "POST": f = request.files["file"] if f: actual_filename = generate_filename(folder_name, f.filename) target_file = os.path.join( os.path.expanduser(config.UPLOAD_FOLDER), actual_filename) if os.path.exists(target_file): return jsonify(message="error"), 409 try: f.save(target_file) f2 = File.create(folder=folder, filename=f.filename, public_share_url=generate_url(), open_public_share=False) f2.save() except Exception as e: print(e) return jsonify(message="error"), 500 return jsonify(message="OK"), 201 if request.method == "DELETE": try: for f in folder.files: actual_filename = generate_filename(folder_name, f.filename) target_file = os.path.join( os.path.expanduser(config.UPLOAD_FOLDER), actual_filename) f2 = File.get(File.filename == f.filename) f2.delete_instance() if os.path.exists(target_file): os.remove(target_file) folder.delete_instance() except Exception as e: return jsonify(message="error"), 409 return jsonify(message="OK")
def add_file(file_name, description=None): """Adds a file name to the catalog""" file_name = file_name.strip() try: date_created = datetime.datetime.fromtimestamp(os.path.getctime(file_name)).strftime('%Y-%m-%d %H:%M:%S') current_file = File(file_name=file_name, description=description, date_created=date_created) current_file.save() print "'%s' added!" % file_name except IntegrityError: current_file = File.get(File.file_name == file_name) return current_file
def init_sync_project_directory(project_id): """ Initial sync of project dir and database. :param project_id: Project id from database. :type project_id: Integer """ log_msg = 'init_sync_project_directory project ID = {project_id}' log_msg = log_msg.format(project_id=project_id) _logger().debug(log_msg) project = Project.get_by_id(project_id) if not project: return project_files = File.get('all', File.project_id == project_id) project_filepaths = [f.path for f in project_files] try: for (root, directories, basenames) in os.walk(project.dir): for filepath in [os.path.join(root, n) for n in basenames]: if filepath in project_filepaths: # Simultaneously remove the element from both lists. index = project_filepaths.index(filepath) project_files.pop(index) project_filepaths.pop(index) else: # Add the new file to project. add_file_to_project(filepath, project_id) # project_files now only contains the files that have been deleted! for file_ in project_files: deleted = REVERSE_ACTIONS['Deleted'] try: controller.create_file_action(file_.path, deleted, 0, project_id) except Exception as excp: _logger().exception('{0!s}'.format(excp)) file_.project_id = None File.update_many(project_files) except OSError as excp: log_msg = ('Exception in Initial project directory synchronization ' 'call: {exception!s}') log_msg = log_msg.format(exception=excp) _logger().exception(log_msg)
def files(folder_name, filename): try: f = File.get(filename=filename) except peewee.DoesNotExist: return jsonify(message="error"), 404 actual_filename = generate_filename(folder_name, filename) target_file = os.path.join(os.path.expanduser(config.UPLOAD_FOLDER), actual_filename) if request.method == "GET": args = request.args if "query" in args and args["query"] == "info": return jsonify(message="OK", data=model_to_dict(f)) if os.path.exists(target_file): return send_file(target_file) else: return jsonify(message="error"), 404 if request.method == "PATCH": share_type = request.args.get("shareType") if share_type == "public": f.open_public_share = True elif share_type == "none": f.open_public_share = False f.save() return jsonify(message="OK") if request.method == "DELETE": if os.path.exists(target_file): try: f.delete_instance() os.remove(target_file) return jsonify(message="OK") except Exception as e: print(e) return jsonify(message="error"), 500 else: return jsonify(message="error"), 404
def update_metadata(self): metadata = File.get(md5=self.preview).get_metadata() # Handle Static markup markup = "" markup += "<b>Name:</b> %s<br />" % metadata['name'] markup += "<b>Path:</b> %s<br />" % metadata['path'] markup += "<b>md5:</b> %s<br />" % metadata['md5'] markup += "<b>Width:</b> %s<br />" % metadata['width'] markup += "<b>Height:</b> %s<br />" % metadata['height'] markup += "<b>Type:</b> %s<br />" % metadata['type'] markup += "<b>size:</b> %s<br />" % metadata['size'] # Handle dynamic Markup fields = defaultdict(set) for m in metadata['metadata']: fields[m.field].add(m.value) for field, values in fields.items(): links = ["<a href='%s:\"%s\"'>%s</a>" % (field, value, value) for value in values] markup += "<b>%s:</b> %s<br />" % (field, ", ".join(links)) self.DetailsWindow.SetPage(markup)
def add_file_to_project(file_path, project_id): """ Add a file to project. Copies it to the folder and adds a record to database. :param file_path: A file_path. :type file_path: String :param project_id: Project id from database. :type projecT_id: Integer :return: New file_path. :rtype: String """ if not file_path: return '' file_path = os.path.abspath(file_path) if File.get('exists', File.path == file_path, File.project_id == project_id): _logger().debug('Not adding file to project: File exists') return '' project = Project.get_by_id(project_id) try: if file_path.startswith(project.dir): newpath = file_path else: newpath = filesystem.copy_file_to_project(file_path, project_id) if newpath: File(newpath, project_id) return newpath except SQLAlchemyError as excp: log_msg = 'Add file to {project!s} exception: {exception!s}' log_msg = log_msg.format(project=project, exception=excp) _logger().exception(log_msg) return ''
def edit_description(file_name): """Edits an existing file's description""" try: f = File.get(File.file_name == file_name.strip()) print "Current %s description: '%s'" % (f.file_name, f.description) choice = raw_input("Replace current description? Y/N: ") if choice.lower() == 'y': new_description = raw_input("Enter new description for '%s': " % f.file_name) f.description = new_description f.save() print "New Description Saved to %s!" % f.file_name else: pass except: print "Whoops. '%s' not in catalog files." % file_name
def get_file_with_id(file_id): file = File.get(File.file_id == file_id) return file
def _OnFileProcedure(self, iterated, filenames): """ Handle filesend in threaded manner. :param iterated: The node. :type iterated: :py:class:`swnp.Node` :param filenames: The filenames to send, can include folder names too. :type filenames: List of String """ if not filenames: return filenames = [unicode(n) for n in filenames] try: deltay = 200 # Used to spawn the boxes in a bit different # positions. for dialogy in self.my_send_dialogs: deltay = deltay if dialogy < deltay else dialogy deltay += 20 self.my_send_dialogs.append(deltay) title = 'Sending items...' project = self.parent.diwa_state.current_project project_items = [] paths = [] if not project: paths.extend(filenames) filenames = [] # Separate project files/folders from other stuff. #: TODO: CHECK PROJECT EXISTANCE!!! for filename in filenames: filename = os.path.abspath(filename) if filename.startswith(diwavars.PROJECT_PATH): project_items.append(filename) if not File.get('exists', File.path == filename): try: File(filename, project.id) except (SQLAlchemyError, ItemAlreadyExistsException): pass else: paths.append(filename) # Process items that are not part of the project. if paths: params = { 'class': SendProgressBar, 'kwargs': { 'parent': self.parent, 'title': title, 'ypos': deltay } } paths = self.parent.diwa_state.handle_file_send(paths, params) # Union the two lists again. paths.extend(project_items) self.parent.Show() self.parent.Raise() self.parent.Update() if paths: filenames = [unicode(p) for p in paths] filenames = [n.encode('utf-8') for n in filenames] command = 'open;{0!s}'.format(filenames) self.parent.diwa_state.swnp_send(str(iterated), command) self.my_send_dialogs.remove(deltay) except Exception as excp: LOGGER.exception('OnData exception: {0} - {1!s}'.\ format(filenames, excp))
def get(self, id): file = File.get(id) if file: return file.response() else: abort(404, error='File not exist.')