Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
IR: 1609: Handle scene renaming with existing scene name (#10255)
Browse files Browse the repository at this point in the history
* fix: handle scene renaming with existing scene name

* fixed tests
  • Loading branch information
MoizAdnan authored May 30, 2024
1 parent cd81c2c commit 7055c76
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/client-core/i18n/en/editor.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"mediaSearchAborted": "Media search aborted",
"methodNotImplemented": "Method not implemented.",
"saveProjectAborted": "Save scene aborted",
"sceneAlreadyExists": "Scene already exists with this name.",
"unsavedChanges": "Unsaved Changes",
"unsavedChangesMsg": "The scene has been modified. Please save your changes.",
"projectCreationFail": "Project creation failed. {{reason}}",
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/assets/ScenesPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export default function ScenesPanel() {
setRenaming(false)
const currentURL = loadedScene!.assetURL
const newURL = currentURL.replace(currentURL.split('/').pop()!, newName + '.gltf')
const newData = await renameScene(id, newURL)
const newData = await renameScene(id, newURL, loadedScene!.projectName)
if (loadedScene) getMutableState(EditorState).scenePath.set(newData.assetURL)
setNewName('')
}
Expand Down
18 changes: 13 additions & 5 deletions packages/editor/src/functions/sceneFunctions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import i18n from 'i18next'

import config from '@etherealengine/common/src/config'
import multiLogger from '@etherealengine/common/src/logger'
import { assetPath } from '@etherealengine/common/src/schema.type.module'
import { AssetType, assetPath } from '@etherealengine/common/src/schema.type.module'
import { cleanString } from '@etherealengine/common/src/utils/cleanString'
import { EntityUUID, UndefinedEntity, UUIDComponent } from '@etherealengine/ecs'
import { EntityUUID, UUIDComponent, UndefinedEntity } from '@etherealengine/ecs'
import { getComponent, getMutableComponent } from '@etherealengine/ecs/src/ComponentFunctions'
import { Engine } from '@etherealengine/ecs/src/Engine'
import { GLTFComponent } from '@etherealengine/engine/src/gltf/GLTFComponent'
Expand All @@ -39,6 +39,7 @@ import { handleScenePaths } from '@etherealengine/engine/src/scene/functions/GLT
import { getMutableState, getState } from '@etherealengine/hyperflux'
import { AssetParams } from '@etherealengine/server-core/src/assets/asset/asset.class'
import { SceneComponent } from '@etherealengine/spatial/src/renderer/components/SceneComponents'
import { Paginated } from '@feathersjs/feathers'
import { EditorState } from '../services/EditorServices'
import { uploadProjectFiles } from './assetFunctions'

Expand All @@ -60,9 +61,9 @@ export const deleteScene = async (sceneID: string): Promise<any> => {
return true
}

export const renameScene = async (id: string, newURL: string, params?: AssetParams) => {
export const renameScene = async (id: string, newURL: string, projectName: string, params?: AssetParams) => {
try {
return await Engine.instance.api.service(assetPath).patch(id, { assetURL: newURL }, params)
return await Engine.instance.api.service(assetPath).patch(id, { assetURL: newURL, project: projectName }, params)
} catch (error) {
logger.error(error, 'Error in renaming project')
throw error
Expand All @@ -83,6 +84,13 @@ export const saveSceneGLTF = async (
const sourceID = `${getComponent(rootEntity, UUIDComponent)}-${getComponent(rootEntity, GLTFComponent).src}`

const sceneName = cleanString(sceneFile!.replace('.scene.json', '').replace('.gltf', ''))
const currentSceneDirectory = getState(EditorState).scenePath!.split('/').slice(0, -1).join('/')

const existingScene = (await Engine.instance.api.service(assetPath).find({
query: { assetURL: `${currentSceneDirectory}/${sceneName}.gltf`, $limit: 1 }
})) as Paginated<AssetType>

if (existingScene.data.length > 0) throw new Error(i18n.t('editor:errors.sceneAlreadyExists'))

const gltfData = getState(GLTFDocumentState)[sourceID]
if (!gltfData) {
Expand All @@ -91,7 +99,7 @@ export const saveSceneGLTF = async (
const encodedGLTF = handleScenePaths(gltfData, 'encode')
const blob = [JSON.stringify(encodedGLTF, null, 2)]
const file = new File(blob, `${sceneName}.gltf`)
const currentSceneDirectory = getState(EditorState).scenePath!.split('/').slice(0, -1).join('/')

const [[newPath]] = await Promise.all(uploadProjectFiles(projectName, [file], [currentSceneDirectory]).promises)

const assetURL = newPath.replace(fileServer, '').slice(1) // remove leading slash
Expand Down
4 changes: 3 additions & 1 deletion packages/server-core/src/assets/asset/asset.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const resolveProjectIdForAssetQuery = async (context: HookContext<AssetService>)
* @returns
*/
export const ensureUniqueName = async (context: HookContext<AssetService>) => {
if (!context.data || context.method !== 'create') {
if (!context.data) {
throw new BadRequest(`${context.path} service only works for data in ${context.method}`)
}

Expand Down Expand Up @@ -328,13 +328,15 @@ export default createSkippableHooks(
iff(isProvider('external'), verifyScope('editor', 'write'), projectPermissionAuthenticate(false)),
schemaHooks.resolveData(assetDataResolver),
resolveProjectIdForAssetData,
ensureUniqueName,
renameAsset,
removeFieldsForAssetData
],
patch: [
iff(isProvider('external'), verifyScope('editor', 'write'), projectPermissionAuthenticate(false)),
schemaHooks.resolveData(assetDataResolver),
resolveProjectIdForAssetData,
ensureUniqueName,
renameAsset,
removeFieldsForAssetData
],
Expand Down
2 changes: 1 addition & 1 deletion packages/server-core/src/assets/asset/asset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('asset.test', () => {
const data = queryResult.data[0]
const updatedData = await app
.service(assetPath)
.patch(data.id, { assetURL: directory + '/Updated-Scene.gltf' }, params)
.patch(data.id, { assetURL: directory + '/Updated-Scene.gltf', project: projectName }, params)
assert.equal(updatedData.assetURL, directory + '/Updated-Scene.gltf')
const storageProvider = getStorageProvider()
assert(storageProvider.doesExist('Updated-Scene.gltf', directory))
Expand Down

0 comments on commit 7055c76

Please sign in to comment.