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
|
user = 0
|
||||||
}
|
}
|
||||||
_, err := a.db.ExecContext(ctx,
|
_, err := a.db.ExecContext(ctx,
|
||||||
"INSERT INTO im_fileuploads(id,createdat,updatedat,userid,status,mimetype,size,etag,location) "+
|
"INSERT INTO im_fileuploads(id,createdat,updatedat,userid,status,mimetype,size,etag,location,original_name) "+
|
||||||
"VALUES(?,?,?,?,?,?,?,?,?)",
|
"VALUES(?,?,?,?,?,?,?,?,?,?)",
|
||||||
store.DecodeUid(fd.Uid()), fd.CreatedAt, fd.UpdatedAt, user,
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4432,8 +4432,8 @@ func (a *adapter) FileFinishUpload(fd *t.FileDef, success bool, size int64) (*t.
|
|||||||
|
|
||||||
now := t.TimeNow()
|
now := t.TimeNow()
|
||||||
if success {
|
if success {
|
||||||
_, err = tx.ExecContext(ctx, "UPDATE im_fileuploads SET updatedat=?,status=?,size=?,etag=?,location=? WHERE id=?",
|
_, 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, store.DecodeUid(fd.Uid()))
|
now, t.UploadCompleted, size, fd.ETag, fd.Location, strings.TrimSpace(fd.OriginalName), store.DecodeUid(fd.Uid()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -4471,11 +4471,11 @@ func (a *adapter) FileGet(fid string) (*t.FileDef, error) {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
var fd t.FileDef
|
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))
|
"FROM im_fileuploads WHERE id=?", store.DecodeUid(id))
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
storageName := id.String32()
|
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)
|
"FROM im_fileuploads WHERE location=? OR location LIKE ? ORDER BY createdat DESC LIMIT 1", storageName, "%/"+storageName)
|
||||||
}
|
}
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
|
|||||||
@@ -5200,8 +5200,8 @@ func (a *adapter) TenantFileStartUpload(id t.TenantID, fd *t.FileDef) error {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
_, err = a.db.ExecContext(ctx,
|
_, err = a.db.ExecContext(ctx,
|
||||||
"INSERT INTO im_fileuploads(tenant_id,id,createdat,updatedat,userid,status,mimetype,size,etag,location) VALUES(?,?,?,?,?,?,?,?,?,?)",
|
"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)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -5232,8 +5232,8 @@ func (a *adapter) TenantFileFinishUpload(id t.TenantID, fd *t.FileDef, success b
|
|||||||
}()
|
}()
|
||||||
now := t.TimeNow()
|
now := t.TimeNow()
|
||||||
if success {
|
if success {
|
||||||
_, err = tx.Exec("UPDATE im_fileuploads SET updatedat=?,status=?,size=?,etag=?,location=? WHERE tenant_id=? AND id=?",
|
_, 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, id, decodeUID(fd.Uid()))
|
now, t.UploadCompleted, size, fd.ETag, fd.Location, strings.TrimSpace(fd.OriginalName), id, decodeUID(fd.Uid()))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
fd.UpdatedAt = now
|
fd.UpdatedAt = now
|
||||||
fd.Size = size
|
fd.Size = size
|
||||||
@@ -5274,12 +5274,12 @@ func (a *adapter) TenantFileGet(id t.TenantID, fid string) (*t.FileDef, error) {
|
|||||||
}
|
}
|
||||||
var fd t.FileDef
|
var fd t.FileDef
|
||||||
err = a.db.GetContext(ctx, &fd,
|
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))
|
"FROM im_fileuploads WHERE tenant_id=? AND id=?", id, decodeUID(fileID))
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
storageName := fileID.String32()
|
storageName := fileID.String32()
|
||||||
err = a.db.GetContext(ctx, &fd,
|
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",
|
"FROM im_fileuploads WHERE tenant_id=? AND (location=? OR location LIKE ?) ORDER BY createdat DESC LIMIT 1",
|
||||||
id, storageName, "%/"+storageName)
|
id, storageName, "%/"+storageName)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1502,9 +1502,8 @@ type FileDef struct {
|
|||||||
User string
|
User string
|
||||||
// Type of the file.
|
// Type of the file.
|
||||||
MimeType string
|
MimeType string
|
||||||
// OriginalName is the client-provided file name. It is not persisted; media handlers use it
|
// OriginalName is the client-provided file name.
|
||||||
// only to keep the public URL extension consistent with the uploaded file.
|
OriginalName string `db:"original_name" bson:"original_name" json:"-"`
|
||||||
OriginalName string `db:"-" bson:"-" json:"-"`
|
|
||||||
// Size of the file in bytes.
|
// Size of the file in bytes.
|
||||||
Size int64
|
Size int64
|
||||||
// Internal file location, i.e. path on disk or an S3 blob address.
|
// Internal file location, i.e. path on disk or an S3 blob address.
|
||||||
|
|||||||
Reference in New Issue
Block a user