{"record":{"id":"3d594da1a9a5513c","repo":"gofr-dev/gofr","slug":"w-new-filename-must-match-the-old-file-s-type","errorCode":null,"errorMessage":"%w: new filename must match the old file's type","messagePattern":"%w: new filename must match the old file's type","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/datasource/file/s3/fs.go","lineNumber":358,"sourceCode":"\t\tf.logger.Logf(\"%q & %q are same\", oldname, newname)\n\t\treturn nil\n\t}\n\n\t// check if both exist at same location or not\n\tif path.Dir(oldname) != path.Dir(newname) {\n\t\tf.logger.Errorf(\"%q & %q are not in same location\", oldname, newname)\n\t\treturn fmt.Errorf(\"%w: renaming as well as moving file to different location is not allowed\", ErrOperationNotPermitted)\n\t}\n\n\t// check if it is a directory\n\tif path.Ext(oldname) == \"\" {\n\t\treturn f.renameDirectory(&st, &msg, oldname, newname)\n\t}\n\n\t// check if they are of the same type or not\n\tif path.Ext(oldname) != path.Ext(newname) {\n\t\tf.logger.Errorf(\"new file must be same as the old file type\")\n\t\treturn fmt.Errorf(\"%w: new filename must match the old file's type\", errIncorrectFileType)\n\t}\n\n\t_, err := f.conn.CopyObject(context.TODO(), &s3.CopyObjectInput{\n\t\tBucket: aws.String(f.config.BucketName),\n\t\t// The source object can be up to 5 GB.\n\t\t// If the source object is an object that was uploaded by using a multipart upload, the object copy\n\t\t// will be a single part object after the source object is copied to the destination bucket.\n\t\tCopySource:         aws.String(f.config.BucketName + \"/\" + oldname),\n\t\tKey:                aws.String(newname),\n\t\tContentType:        aws.String(mime.TypeByExtension(path.Ext(newname))),\n\t\tContentDisposition: aws.String(\"attachment\"),\n\t})\n\tif err != nil {\n\t\tmsg = fmt.Sprintf(\"Error while copying file: %v\", err)\n\t\treturn err\n\t}\n\n\terr = f.Remove(oldname)","sourceCodeStart":340,"sourceCodeEnd":376,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/file/s3/fs.go#L340-L376","documentation":"Rename wraps errIncorrectFileType when the extension of the new name differs from the old one, with the message 'new filename must match the old file's type'. Since S3 renames are copy+delete, the library refuses to silently transform what looks like a different kind of object.","triggerScenarios":"Rename(oldname, newname) on a file (oldname has an extension) where path.Ext(oldname) != path.Ext(newname), e.g. Rename(\"bucket/data.csv\", \"bucket/data.parquet\").","commonSituations":"Accidentally appending/replacing extensions during a rename; building generic rename helpers that accept arbitrary new names; users expecting rename to double as format conversion.","solutions":["Preserve path.Ext(oldname) when constructing the new name.","If conversion is intended, do it explicitly: read, transform, write to the new key, then delete the old object.","Validate names before calling Rename so the caller sees its own validation error instead of this sentinel."],"exampleFix":"// before\nnewName := \"data-2026.json\"\nfs.Rename(\"bucket/data.json.bak\", \"bucket/\"+newName) // type mismatch\n// after\nnewName := \"data-2026.json.bak\"\nfs.Rename(\"bucket/data.json.bak\", \"bucket/\"+newName)","handlingStrategy":"validation","validationCode":"func sameExt(old, new string) bool { return path.Ext(old) == path.Ext(new) }\n// guard:\nif !sameExt(oldName, newName) { /* adjust newName or use explicit conversion */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Preserve the source extension when generating new names.","Do conversions with explicit read/transform/write pipelines.","Unit-test name-generation helpers for extension preservation.","Document that Rename never changes file type."],"tags":["rename","file-type","s3","gofr"],"backgroundTag":"type-mismatch","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}