Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend File.write() to optionally write files to specific folders #1217

Open
phorward opened this issue Jul 18, 2024 · 1 comment
Open

Extend File.write() to optionally write files to specific folders #1217

phorward opened this issue Jul 18, 2024 · 1 comment
Assignees
Labels
feature New feature or request help wanted Extra attention is needed proposal

Comments

@phorward
Copy link
Member

In a customer project (viur-core 3.5), I've written this function some time ago. It writes a file to the File module, but also to a specific location. This can be seen as an extension for File.write(), so that it should be generalized and integrated into viur-core.

class File(_File):

	def write_to_folder(self, foldername, filename, content, mimetype="text/plain"):
		"""
		Write a file into the file module to a specified folder.
		The foldername is both the folder's initial name and name stored as the key.
		The folder can be renamed later on by the user, but the key remains.
		The folder is created under the module root node.
		"""
		# Check for foldername
		rootnode = self.ensureOwnModuleRootNode()

		folderskel = self.baseSkel("node")
		folderkey = db.Key(folderskel.kindName, foldername)

		# Create the folder if it doesn't exist
		if not folderskel.fromDB(folderkey):
			folderskel["key"] = folderkey
			folderskel["name"] = foldername
			folderskel["parentrepo"] = folderskel["parententry"] = rootnode.key
			folderskel.toDB()

		# Save the file into the folder.
		dl_key = utils.generateRandomString()

		blob = bucket.blob("%s/source/%s" % (dl_key, filename))
		blob.upload_from_file(io.BytesIO(content), content_type=mimetype)

		skel = self.addSkel("leaf")

		skel["name"] = filename
		skel["size"] = blob.size
		skel["mimetype"] = mimetype
		skel["dlkey"] = dl_key
		skel["parentrepo"] = rootnode.key
		skel["parententry"] = folderkey
		skel["pending"] = False
		skel["weak"] = False
		skel["width"] = 0
		skel["height"] = 0

		assert skel.toDB()
		return skel
@phorward phorward added feature New feature or request help wanted Extra attention is needed proposal labels Jul 18, 2024
@phorward
Copy link
Member Author

Some further ideas on this issue:

  • it should be an extension to File.write()
  • it should accept for a root-node, and default to self.ensureOwnModuleRootNode()
  • it should also accept a path of folders as an iterable of str, so ("this", "is", "a", "valid path"). The folders should be created when not existing.

@phorward phorward changed the title Provide way to write a file to a specific folder File-module: Write file to a specific folder Jul 18, 2024
@phorward phorward changed the title File-module: Write file to a specific folder Extend File.write() to optionally write files to specific folders Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request help wanted Extra attention is needed proposal
Projects
None yet
Development

No branches or pull requests

2 participants