fix: persist upload original filename
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
-- Persist client-provided upload filename for downstream business modules.
|
||||
-- Apply before deploying im_server changes which read/write im_fileuploads.original_name.
|
||||
|
||||
ALTER TABLE im_fileuploads
|
||||
ADD COLUMN original_name VARCHAR(255) NOT NULL DEFAULT '' AFTER location;
|
||||
@@ -4407,10 +4407,10 @@ func (a *adapter) FileStartUpload(fd *t.FileDef) error {
|
||||
user = 0
|
||||
}
|
||||
_, err := a.db.ExecContext(ctx,
|
||||
"INSERT INTO im_fileuploads(id,createdat,updatedat,userid,status,mimetype,size,etag,location) "+
|
||||
"VALUES(?,?,?,?,?,?,?,?,?)",
|
||||
"INSERT INTO im_fileuploads(id,createdat,updatedat,userid,status,mimetype,size,etag,location,original_name) "+
|
||||
"VALUES(?,?,?,?,?,?,?,?,?,?)",
|
||||
store.DecodeUid(fd.Uid()), fd.CreatedAt, fd.UpdatedAt, user,
|
||||
fd.Status, fd.MimeType, fd.Size, fd.ETag, fd.Location)
|
||||
fd.Status, fd.MimeType, fd.Size, fd.ETag, fd.Location, strings.TrimSpace(fd.OriginalName))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -4432,8 +4432,8 @@ func (a *adapter) FileFinishUpload(fd *t.FileDef, success bool, size int64) (*t.
|
||||
|
||||
now := t.TimeNow()
|
||||
if success {
|
||||
_, err = tx.ExecContext(ctx, "UPDATE im_fileuploads SET updatedat=?,status=?,size=?,etag=?,location=? WHERE id=?",
|
||||
now, t.UploadCompleted, size, fd.ETag, fd.Location, store.DecodeUid(fd.Uid()))
|
||||
_, err = tx.ExecContext(ctx, "UPDATE im_fileuploads SET updatedat=?,status=?,size=?,etag=?,location=?,original_name=? WHERE id=?",
|
||||
now, t.UploadCompleted, size, fd.ETag, fd.Location, strings.TrimSpace(fd.OriginalName), store.DecodeUid(fd.Uid()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -4471,11 +4471,11 @@ func (a *adapter) FileGet(fid string) (*t.FileDef, error) {
|
||||
defer cancel()
|
||||
}
|
||||
var fd t.FileDef
|
||||
err := a.db.GetContext(ctx, &fd, "SELECT id,createdat,updatedat,userid AS user,status,mimetype,size,IFNULL(etag,'') AS etag,location "+
|
||||
err := a.db.GetContext(ctx, &fd, "SELECT id,createdat,updatedat,userid AS user,status,mimetype,size,IFNULL(etag,'') AS etag,location,IFNULL(original_name,'') AS original_name "+
|
||||
"FROM im_fileuploads WHERE id=?", store.DecodeUid(id))
|
||||
if err == sql.ErrNoRows {
|
||||
storageName := id.String32()
|
||||
err = a.db.GetContext(ctx, &fd, "SELECT id,createdat,updatedat,userid AS user,status,mimetype,size,IFNULL(etag,'') AS etag,location "+
|
||||
err = a.db.GetContext(ctx, &fd, "SELECT id,createdat,updatedat,userid AS user,status,mimetype,size,IFNULL(etag,'') AS etag,location,IFNULL(original_name,'') AS original_name "+
|
||||
"FROM im_fileuploads WHERE location=? OR location LIKE ? ORDER BY createdat DESC LIMIT 1", storageName, "%/"+storageName)
|
||||
}
|
||||
if err == sql.ErrNoRows {
|
||||
|
||||
@@ -5200,8 +5200,8 @@ func (a *adapter) TenantFileStartUpload(id t.TenantID, fd *t.FileDef) error {
|
||||
defer cancel()
|
||||
}
|
||||
_, err = a.db.ExecContext(ctx,
|
||||
"INSERT INTO im_fileuploads(tenant_id,id,createdat,updatedat,userid,status,mimetype,size,etag,location) VALUES(?,?,?,?,?,?,?,?,?,?)",
|
||||
id, decodeUID(fd.Uid()), fd.CreatedAt, fd.UpdatedAt, user, fd.Status, fd.MimeType, fd.Size, fd.ETag, fd.Location)
|
||||
"INSERT INTO im_fileuploads(tenant_id,id,createdat,updatedat,userid,status,mimetype,size,etag,location,original_name) VALUES(?,?,?,?,?,?,?,?,?,?,?)",
|
||||
id, decodeUID(fd.Uid()), fd.CreatedAt, fd.UpdatedAt, user, fd.Status, fd.MimeType, fd.Size, fd.ETag, fd.Location, strings.TrimSpace(fd.OriginalName))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -5232,8 +5232,8 @@ func (a *adapter) TenantFileFinishUpload(id t.TenantID, fd *t.FileDef, success b
|
||||
}()
|
||||
now := t.TimeNow()
|
||||
if success {
|
||||
_, err = tx.Exec("UPDATE im_fileuploads SET updatedat=?,status=?,size=?,etag=?,location=? WHERE tenant_id=? AND id=?",
|
||||
now, t.UploadCompleted, size, fd.ETag, fd.Location, id, decodeUID(fd.Uid()))
|
||||
_, err = tx.Exec("UPDATE im_fileuploads SET updatedat=?,status=?,size=?,etag=?,location=?,original_name=? WHERE tenant_id=? AND id=?",
|
||||
now, t.UploadCompleted, size, fd.ETag, fd.Location, strings.TrimSpace(fd.OriginalName), id, decodeUID(fd.Uid()))
|
||||
if err == nil {
|
||||
fd.UpdatedAt = now
|
||||
fd.Size = size
|
||||
@@ -5274,12 +5274,12 @@ func (a *adapter) TenantFileGet(id t.TenantID, fid string) (*t.FileDef, error) {
|
||||
}
|
||||
var fd t.FileDef
|
||||
err = a.db.GetContext(ctx, &fd,
|
||||
"SELECT tenant_id,id,createdat,updatedat,userid AS user,status,mimetype,size,IFNULL(etag,'') AS etag,location "+
|
||||
"SELECT tenant_id,id,createdat,updatedat,userid AS user,status,mimetype,size,IFNULL(etag,'') AS etag,location,IFNULL(original_name,'') AS original_name "+
|
||||
"FROM im_fileuploads WHERE tenant_id=? AND id=?", id, decodeUID(fileID))
|
||||
if err == sql.ErrNoRows {
|
||||
storageName := fileID.String32()
|
||||
err = a.db.GetContext(ctx, &fd,
|
||||
"SELECT tenant_id,id,createdat,updatedat,userid AS user,status,mimetype,size,IFNULL(etag,'') AS etag,location "+
|
||||
"SELECT tenant_id,id,createdat,updatedat,userid AS user,status,mimetype,size,IFNULL(etag,'') AS etag,location,IFNULL(original_name,'') AS original_name "+
|
||||
"FROM im_fileuploads WHERE tenant_id=? AND (location=? OR location LIKE ?) ORDER BY createdat DESC LIMIT 1",
|
||||
id, storageName, "%/"+storageName)
|
||||
}
|
||||
|
||||
@@ -1502,9 +1502,8 @@ type FileDef struct {
|
||||
User string
|
||||
// Type of the file.
|
||||
MimeType string
|
||||
// OriginalName is the client-provided file name. It is not persisted; media handlers use it
|
||||
// only to keep the public URL extension consistent with the uploaded file.
|
||||
OriginalName string `db:"-" bson:"-" json:"-"`
|
||||
// OriginalName is the client-provided file name.
|
||||
OriginalName string `db:"original_name" bson:"original_name" json:"-"`
|
||||
// Size of the file in bytes.
|
||||
Size int64
|
||||
// Internal file location, i.e. path on disk or an S3 blob address.
|
||||
|
||||
Reference in New Issue
Block a user