@ -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.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.FORMAT_SS ;
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.spi.enums.ResourceType ;
import org.apache.commons.beanutils.BeanMap ;
import org.apache.commons.collections.CollectionUtils ;
import org.apache.commons.lang3.StringUtils ;
@ -1499,103 +1497,25 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe
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
@Transactional
public Result < Object > onlineCreateOrUpdateResourceWithDir ( User loginUser , String fileFullName , String desc ,
String content ) {
// 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 ) {
public StorageEntity createOrUpdateResource ( String userName , String filepath ,
String resourceContent ) throws Exception {
User user = userMapper . queryByUserNameAccurately ( userName ) ;
int suffixLabelIndex = fullName . indexOf ( PERIOD ) ;
int suffixLabelIndex = filepath . indexOf ( PERIOD ) ;
if ( suffixLabelIndex = = - 1 ) {
String msg = String . format ( "The suffix of file can not be empty, fullName:%s." , fullName ) ;
logger . warn ( msg ) ;
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 dirFullName = currentDir + FOLDER_SEPARATOR + dirName ;
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");
throw new IllegalArgumentException ( String
. format ( "Not allow create or update resources without extension name, filepath: %s" , filepath ) ) ;
}
// Since resource is kept in third party services, its id will always be -1.
return - 1 ;
String defaultPath = storageOperate . getResDir ( user . getTenantCode ( ) ) ;
String fullName = defaultPath + filepath ;
} else {
String msg = String . format ( "Create dir error, dirFullName:%s." , dirFullName ) ;
logger . error ( msg ) ;
throw new IllegalArgumentException ( msg ) ;
}
Result < Object > result = uploadContentToStorage ( user , fullName , user . getTenantCode ( ) , resourceContent ) ;
if ( result . getCode ( ) ! = Status . SUCCESS . getCode ( ) ) {
throw new ServiceException ( result . getMsg ( ) ) ;
}
return storageOperate . getFileStatus ( fullName , defaultPath , user . getTenantCode ( ) , ResourceType . FILE ) ;
}
private void permissionPostHandle ( ResourceType resourceType , User loginUser , Integer resourceId ) {
@ -1864,17 +1784,13 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe
}
@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
User user = userMapper . queryByUserNameAccurately ( userName ) ;
Result < Object > resourceResponse = this . queryResourceByFileName ( user , fileName , ResourceType . FILE , "" ) ;
if ( resourceResponse . getCode ( ) ! = Status . SUCCESS . getCode ( ) ) {
String msg =
String . format ( "Query resource by fullName failed, userName:%s, fullName:%s" , userName , fileName ) ;
logger . error ( msg ) ;
throw new IllegalArgumentException ( msg ) ;
}
return ( Resource ) resourceResponse . getData ( ) ;
String defaultPath = storageOperate . getResDir ( user . getTenantCode ( ) ) ;
return storageOperate . getFileStatus ( defaultPath + fileName , defaultPath , user . getTenantCode ( ) ,
ResourceType . FILE ) ;
}
@Override