Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid insert file with blank filename #3774

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion inc/INIT.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class INIT {

public static $COMMENTS_ENABLED = true;
public static string $SOCKET_NOTIFICATIONS_QUEUE_NAME = "/queue/matecat_socket_notifications";
public static string $SOCKET_BASE_URL;
public static string $SOCKET_BASE_URL = '';

public static $SMTP_HOST;
public static $SMTP_PORT;
Expand Down
5 changes: 5 additions & 0 deletions lib/Model/FilesStorage/AbstractFilesStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ protected function _linkToCache( $dir, $hash, $realFileName ): int {
$content[] = $realFileName;
}

// remove empty lines
$content = array_filter($content, function ($filename){
return !empty($filename);
});

$contentString = implode( "\n", $content ) . "\n";

$bytesWritten = fwrite( $fp, $contentString );
Expand Down
52 changes: 28 additions & 24 deletions lib/Utils/ProjectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2312,41 +2312,45 @@ protected function _insertFiles( $_originalFileNames, $sha1_original, $cachedXli

foreach ( $_originalFileNames as $pos => $originalFileName ) {

// get metadata
$meta = isset( $this->projectStructure[ 'array_files_meta' ][ $pos ] ) ? $this->projectStructure[ 'array_files_meta' ][ $pos ] : null;
// avoid blank filenames
if(!empty($originalFileName)){

$cachedXliffFileName = AbstractFilesStorage::pathinfo_fix($cachedXliffFilePathName, PATHINFO_FILENAME);
$mimeType = AbstractFilesStorage::pathinfo_fix( $originalFileName, PATHINFO_EXTENSION );
$fid = ProjectManagerModel::insertFile( $this->projectStructure, $originalFileName, $mimeType, $fileDateSha1Path, $meta );
// get metadata
$meta = isset( $this->projectStructure[ 'array_files_meta' ][ $pos ] ) ? $this->projectStructure[ 'array_files_meta' ][ $pos ] : null;

if ( $this->gdriveSession ) {
$gdriveFileId = $this->gdriveSession->findFileIdByName( $originalFileName );
if ( $gdriveFileId ) {
$client = GoogleProvider::getClient( INIT::$HTTPHOST . "/gdrive/oauth/response" );
$this->gdriveSession->createRemoteFile( $fid, $gdriveFileId, $client );
$cachedXliffFileName = AbstractFilesStorage::pathinfo_fix($cachedXliffFilePathName, PATHINFO_FILENAME);
$mimeType = AbstractFilesStorage::pathinfo_fix( $originalFileName, PATHINFO_EXTENSION );
$fid = ProjectManagerModel::insertFile( $this->projectStructure, $originalFileName, $mimeType, $fileDateSha1Path, $meta );

if ( $this->gdriveSession ) {
$gdriveFileId = $this->gdriveSession->findFileIdByName( $originalFileName );
if ( $gdriveFileId ) {
$client = GoogleProvider::getClient( INIT::$HTTPHOST . "/gdrive/oauth/response" );
$this->gdriveSession->createRemoteFile( $fid, $gdriveFileId, $client );
}
}
}

$moved = $fs->moveFromCacheToFileDir(
$moved = $fs->moveFromCacheToFileDir(
$fileDateSha1Path,
$this->projectStructure[ 'source_language' ],
$fid,
$originalFileName
);
);

// check if the files were moved
if ( true !== $moved ) {
throw new Exception( 'Project creation failed. Please refresh page and retry.', -200 );
}
// check if the files were moved
if ( true !== $moved ) {
throw new Exception( 'Project creation failed. Please refresh page and retry.', -200 );
}

$this->projectStructure[ 'file_id_list' ]->append( $fid );
$this->projectStructure[ 'file_id_list' ]->append( $fid );

$fileStructures[ $fid ] = [
'fid' => $fid,
'original_filename' => $originalFileName,
'path_cached_xliff' => $cachedXliffFilePathName,
'mime_type' => $mimeType
];
$fileStructures[ $fid ] = [
'fid' => $fid,
'original_filename' => $originalFileName,
'path_cached_xliff' => $cachedXliffFilePathName,
'mime_type' => $mimeType
];
}
}

return $fileStructures;
Expand Down