@ -19,7 +19,6 @@ package org.apache.dolphinscheduler.api.service.impl;
import static org.apache.dolphinscheduler.common.constants.Constants.ALIAS ;
import static org.apache.dolphinscheduler.common.constants.Constants.ALIAS ;
import static org.apache.dolphinscheduler.common.constants.Constants.CONTENT ;
import static org.apache.dolphinscheduler.common.constants.Constants.CONTENT ;
import static org.apache.dolphinscheduler.common.constants.Constants.EMPTY_STRING ;
import static org.apache.dolphinscheduler.common.constants.Constants.FOLDER_SEPARATOR ;
import static org.apache.dolphinscheduler.common.constants.Constants.FOLDER_SEPARATOR ;
import static org.apache.dolphinscheduler.common.constants.Constants.FORMAT_SS ;
import static org.apache.dolphinscheduler.common.constants.Constants.FORMAT_SS ;
import static org.apache.dolphinscheduler.common.constants.Constants.FORMAT_S_S ;
import static org.apache.dolphinscheduler.common.constants.Constants.FORMAT_S_S ;
@ -68,7 +67,6 @@ import org.apache.dolphinscheduler.service.storage.StorageEntity;
import org.apache.dolphinscheduler.service.storage.StorageOperate ;
import org.apache.dolphinscheduler.service.storage.StorageOperate ;
import org.apache.dolphinscheduler.spi.enums.ResourceType ;
import org.apache.dolphinscheduler.spi.enums.ResourceType ;
import org.apache.commons.beanutils.BeanMap ;
import org.apache.commons.collections.CollectionUtils ;
import org.apache.commons.collections.CollectionUtils ;
import org.apache.commons.lang3.StringUtils ;
import org.apache.commons.lang3.StringUtils ;
@ -1499,103 +1497,25 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe
return result ;
return result ;
}
}
/ * *
* create or update resource .
* If the folder is not already created , it will be
*
* @param loginUser user who create or update resource
* @param fileFullName The full name of resource . Includes path and suffix .
* @param desc description of resource
* @param content content of resource
* @return create result code
* /
@Override
@Override
@Transactional
@Transactional
public Result < Object > onlineCreateOrUpdateResourceWithDir ( User loginUser , String fileFullName , String desc ,
public StorageEntity createOrUpdateResource ( String userName , String filepath ,
String content ) {
String resourceContent ) throws Exception {
// TODO: need update to third party service
if ( checkResourceExists ( fileFullName ) ) {
Resource resource = resourcesMapper . queryResource ( fileFullName , ResourceType . FILE . ordinal ( ) ) . get ( 0 ) ;
Result < Object > result = this . updateResourceContent ( loginUser , fileFullName ,
resource . getUserName ( ) , content ) ;
if ( result . getCode ( ) = = Status . SUCCESS . getCode ( ) ) {
resource . setDescription ( desc ) ;
Map < String , Object > resultMap = new HashMap < > ( ) ;
for ( Map . Entry < Object , Object > entry : new BeanMap ( resource ) . entrySet ( ) ) {
if ( ! Constants . CLASS . equalsIgnoreCase ( entry . getKey ( ) . toString ( ) ) ) {
resultMap . put ( entry . getKey ( ) . toString ( ) , entry . getValue ( ) ) ;
}
}
result . setData ( resultMap ) ;
}
return result ;
} else {
String resourceSuffix = fileFullName . substring ( fileFullName . indexOf ( PERIOD ) + 1 ) ;
String fileNameWithSuffix = fileFullName . substring ( fileFullName . lastIndexOf ( FOLDER_SEPARATOR ) + 1 ) ;
String resourceDir = fileFullName . replace ( fileNameWithSuffix , EMPTY_STRING ) ;
String resourceName = fileNameWithSuffix . replace ( PERIOD + resourceSuffix , EMPTY_STRING ) ;
String [ ] dirNames = resourceDir . split ( FOLDER_SEPARATOR ) ;
int pid = - 1 ;
StringBuilder currDirPath = new StringBuilder ( ) ;
for ( String dirName : dirNames ) {
if ( StringUtils . isNotEmpty ( dirName ) ) {
pid = queryOrCreateDirId ( loginUser , pid , currDirPath . toString ( ) , dirName ) ;
currDirPath . append ( FOLDER_SEPARATOR ) . append ( dirName ) ;
}
}
return this . onlineCreateResource (
loginUser , ResourceType . FILE , resourceName , resourceSuffix , desc , content ,
currDirPath . toString ( ) ) ;
}
}
@Override
@Transactional
public Integer createOrUpdateResource ( String userName , String fullName , String description ,
String resourceContent ) {
User user = userMapper . queryByUserNameAccurately ( userName ) ;
User user = userMapper . queryByUserNameAccurately ( userName ) ;
int suffixLabelIndex = fullName . indexOf ( PERIOD ) ;
int suffixLabelIndex = filepath . indexOf ( PERIOD ) ;
if ( suffixLabelIndex = = - 1 ) {
if ( suffixLabelIndex = = - 1 ) {
String msg = String . format ( "The suffix of file can not be empty, fullName:%s." , fullName ) ;
throw new IllegalArgumentException ( String
logger . warn ( msg ) ;
. format ( "Not allow create or update resources without extension name, filepath: %s" , filepath ) ) ;
throw new IllegalArgumentException ( msg ) ;
}
if ( ! fullName . startsWith ( FOLDER_SEPARATOR ) ) {
fullName = FOLDER_SEPARATOR + fullName ;
}
Result < Object > createResult = onlineCreateOrUpdateResourceWithDir (
user , fullName , description , resourceContent ) ;
if ( createResult . getCode ( ) = = Status . SUCCESS . getCode ( ) ) {
Map < String , Object > resultMap = ( Map < String , Object > ) createResult . getData ( ) ;
return ( int ) resultMap . get ( "id" ) ;
}
String msg = String . format ( "Create or update resource error, resourceName:%s." , fullName ) ;
logger . error ( msg ) ;
throw new IllegalArgumentException ( msg ) ;
}
}
private int queryOrCreateDirId ( User user , int pid , String currentDir , String dirName ) {
String defaultPath = storageOperate . getResDir ( user . getTenantCode ( ) ) ;
String dirFullName = currentDir + FOLDER_SEPARATOR + dirName ;
String fullName = defaultPath + filepath ;
if ( checkResourceExists ( dirFullName ) ) {
List < Resource > resourceList = resourcesMapper . queryResource ( dirFullName , ResourceType . FILE . ordinal ( ) ) ;
return resourceList . get ( 0 ) . getId ( ) ;
} else {
// create dir
Result < Object > createDirResult = this . createDirectory (
user , dirName , EMPTY_STRING , ResourceType . FILE , pid , currentDir ) ;
if ( createDirResult . getCode ( ) = = Status . SUCCESS . getCode ( ) ) {
// Map<String, Object> resultMap = (Map<String, Object>) createDirResult.getData();
// return resultMap.get("id") == null ? -1 : (Integer) resultMap.get("id");
// Since resource is kept in third party services, its id will always be -1.
return - 1 ;
} else {
Result < Object > result = uploadContentToStorage ( user , fullName , user . getTenantCode ( ) , resourceContent ) ;
String msg = String . format ( "Create dir error, dirFullName:%s." , dirFullName ) ;
if ( result . getCode ( ) ! = Status . SUCCESS . getCode ( ) ) {
logger . error ( msg ) ;
throw new ServiceException ( result . getMsg ( ) ) ;
throw new IllegalArgumentException ( msg ) ;
}
}
}
return storageOperate . getFileStatus ( fullName , defaultPath , user . getTenantCode ( ) , ResourceType . FILE ) ;
}
}
private void permissionPostHandle ( ResourceType resourceType , User loginUser , Integer resourceId ) {
private void permissionPostHandle ( ResourceType resourceType , User loginUser , Integer resourceId ) {
@ -1864,17 +1784,13 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe
}
}
@Override
@Override
public Resource queryResourcesFileInfo ( String userName , String fileName ) {
public StorageEntity queryFileStatus ( String userName , String fileName ) throws Exception {
// TODO: It is used in PythonGateway, should be revised
// TODO: It is used in PythonGateway, should be revised
User user = userMapper . queryByUserNameAccurately ( userName ) ;
User user = userMapper . queryByUserNameAccurately ( userName ) ;
Result < Object > resourceResponse = this . queryResourceByFileName ( user , fileName , ResourceType . FILE , "" ) ;
if ( resourceResponse . getCode ( ) ! = Status . SUCCESS . getCode ( ) ) {
String defaultPath = storageOperate . getResDir ( user . getTenantCode ( ) ) ;
String msg =
return storageOperate . getFileStatus ( defaultPath + fileName , defaultPath , user . getTenantCode ( ) ,
String . format ( "Query resource by fullName failed, userName:%s, fullName:%s" , userName , fileName ) ;
ResourceType . FILE ) ;
logger . error ( msg ) ;
throw new IllegalArgumentException ( msg ) ;
}
return ( Resource ) resourceResponse . getData ( ) ;
}
}
@Override
@Override