CARVIEW |
Navigation Menu
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Copying a file from one collection to another in a hook (preferably JS) #6792
-
If someone has the answer in Go I will be very grateful for that also. I am trying to copy a record from the files collection (where the file is stored on the "file" key) into the fileVersionHistory collection. All fields will stay the same other than a new id is assigned and the old will become a relation to the file that has been copied. My problem is that when using
|
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment · 3 replies
-
const fullPath = $app.dataDir() + "/storage/" + record.baseFilesPath() + "/" + record.get("file") If you want a more universal solution that will work with both the local filesystem and S3 then you'll have to use the filesystem abstraction apis - https://pocketbase.io/docs/js-filesystem/#reading-files. The only caveat is that the JSVM currently doesn't have exposed helpers to work efficiently with const filename = record.get("file")
const fileKey = record.baseFilesPath() + "/" + filename
let fsys, reader;
try {
// initialize the filesystem
fsys = $app.newFilesystem();
// retrieve a file reader for the file key
//
// note: this method will be soft-deprecated and renamed to getReader to avoid confusion with $filesystem.File
// in v0.28.0+ release (it is in the develop branch but there are no ETAs yet)
reader = fsys.getFile(fileKey)
// create a $filesystem.File instance from the reader's content
const file = $filesystem.fileFromBytes(toString(reader), "")
// overwrite the name fields to be the same as the original one
file.name = filename
file.originalName = file.name
const collection = $app.findCollectionByNameOrId("filesVersionHistory");
const record = new Record(collection);
record.set("file", file);
record.set("fileRelationId", id);
record.set("directoryRelationId", e.record.get("directoryRelationId"));
...
} finally {
reader?.close();
fsys?.close();
} (I'll consider in the future to expose to the JSVM methods to construct The equivalent Go filesystem APIs could be found in https://pocketbase.io/docs/go-filesystem/#reading-files. Side-note: if you want to access the old/original record fields you can call Edit: updated the example with the correct |
Beta Was this translation helpful? Give feedback.
All reactions
-
β€οΈ 1
-
Thanks Gani, I'll try this thanks. Pocketbase is amazing, thanks so much for what you've made! |
Beta Was this translation helpful? Give feedback.
All reactions
-
Just for info in case someone stumble here: I've added in the const filename = record.get("file")
const fileKey = record.baseFilesPath() + "/" + filename
let fsys;
try {
// initialize the filesystem
fsys = $app.newFilesystem();
// avaialble with v0.28.0+
const file = fsys.getReuploadableFile(fileKey, true)
const collection = $app.findCollectionByNameOrId("filesVersionHistory");
const record = new Record(collection);
record.set("file", file);
record.set("fileRelationId", id);
record.set("directoryRelationId", e.record.get("directoryRelationId"));
...
} finally {
fsys?.close();
} Under the hood, it will send a call to retrieve first the attributes (size, original name) of the provided fileKey and then wrap its reader as ready to be reuploaded |
Beta Was this translation helpful? Give feedback.
All reactions
-
π 1
-
Just FYI this worked. Here is the final code;
|
Beta Was this translation helpful? Give feedback.
$filesystem.fileFromPath(fullPath)
expects a valid path to a file from the local filesystem. If you are not using S3, you can make it work by constructing the full path to the file, aka.:If you want a more universal solution that will work with both the local filesystem and S3 then you'll have to use the filesystem abstraction apis - https://pocketbase.io/docs/js-filesystem/#reading-files. The only caveat is that the JSVM currently doesn't have exposed helpers to work efficiently with
io.Reader
values and the file will be read entirely in memory for the request duration which could be a probβ¦