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

Display image files #126

Open
wants to merge 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function execute($request)
$fileBreadCrumbList = FileQuery::create()
->filterById($fileId, Criteria::NOT_EQUAL)
->filterByBranchId($this->currentBreadCrumbFile->getBranchId())
->filterByIsBinary(false)
->orderByFilename()
->find()
;
Expand Down
62 changes: 42 additions & 20 deletions apps/front/modules/default/actions/fileAction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function execute($request)
->select(array('Id', 'Filename'))
->filterByBranchId($this->file->getBranchId())
->filterByFilename($this->file->getFilename(), Criteria::LESS_THAN)
->filterByIsBinary(false)
->orderByFilename(Criteria::DESC)
->find()
;
Expand All @@ -44,12 +43,10 @@ public function execute($request)
->select(array('Id', 'Filename'))
->filterByBranchId($this->file->getBranchId())
->filterByFilename($this->file->getFilename(), Criteria::GREATER_THAN)
->filterByIsBinary(false)
->orderByFilename(Criteria::ASC)
->find()
;


$commitFrom = $request->getParameter('from', $this->branch->getCommitReference());
$commitTo = $request->getParameter('to', $this->branch->getLastCommit());
$this->commit_from = null;
Expand Down Expand Up @@ -77,28 +74,53 @@ public function execute($request)
$this->previousFileId = $this->findClosestFileId($previousFiles, $modifiedFiles);
$this->nextFileId = $this->findClosestFileId($nextFiles, $modifiedFiles);

$this->fileContentLines = $this->gitCommand->getShowFileFromBranch(
$this->repository->getGitDir(),
$commitFrom,
$commitTo,
$this->file->getFilename(),
$options
);
if ($this->file->getIsBinary())
{
$oldBinaryContent = $this->gitCommand->getShowFile(
$this->repository->getGitDir(),
$commitFrom,
$this->file->getFilename()
);

$fileLineCommentsModel = CommentQuery::create()
->filterByFileId($this->file->getId())
->filterByCommit($this->file->getLastChangeCommit())
->filterByType(CommentPeer::TYPE_LINE)
->find()
;
$this->oldImageExists = !(strpos($oldBinaryContent, 'fatal: Path') === 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initialiser ces paramètres ? pour toujours les avoir même quand on ne passe (même si dans la template on semble passer par une condition équivalente sur getIsBinary)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

et puis, c'est pas un peu triché cette detection par rapport au message git pour déterminer si le fichier est nouveau :o ?

$this->oldImageType = ImageUtils::getImageTypeFromContent($oldBinaryContent);
$this->oldImageContent = base64_encode($oldBinaryContent);

$this->userId = $this->getUser()->getId();
$newBinaryContent = $this->gitCommand->getShowFile(
$this->repository->getGitDir(),
$commitTo,
$this->file->getFilename()
);

$this->fileLineComments = array();
foreach ($fileLineCommentsModel as $fileLineCommentModel)
$this->newImageExists = !(strpos($newBinaryContent, 'fatal: Path') === 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

factorisation avec le bloc du dessus ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ça ne devrait pas être au getShowFile de faire ce test ? (pour ne pas vérifier la présence de cette chaine dans l'action).

$this->newImageType = ImageUtils::getImageTypeFromContent($newBinaryContent);
$this->newImageContent = base64_encode($newBinaryContent);
}
else
{
$this->fileLineComments[$fileLineCommentModel->getPosition()][] = $fileLineCommentModel;
$this->fileContentLines = $this->gitCommand->getShowFileFromBranch(
$this->repository->getGitDir(),
$commitFrom,
$commitTo,
$this->file->getFilename(),
$options
);

$fileLineCommentsModel = CommentQuery::create()
->filterByFileId($this->file->getId())
->filterByCommit($this->file->getLastChangeCommit())
->filterByType(CommentPeer::TYPE_LINE)
->find()
;

$this->fileLineComments = array();
foreach ($fileLineCommentsModel as $fileLineCommentModel)
{
$this->fileLineComments[$fileLineCommentModel->getPosition()][] = $fileLineCommentModel;
}
}

$this->userId = $this->getUser()->getId();
}

/**
Expand Down
15 changes: 7 additions & 8 deletions apps/front/modules/default/templates/fileListSuccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@
<td class="file_name">
<h3>
<?php if ($file['ReviewRequest'] == 1): ?><span class="ricon">i</span><?php endif; ?>
<?php if (!$file['IsBinary']):?>
<a
class="tooltip"
href="<?php echo url_for("default/file?" . http_build_query(array_merge($defaultParametersUrlFile, array('file' => $file['Id'])))); ?>"
title="<?php echo stringUtils::trimTicketInfos($file['LastChangeCommitDesc'])?>">
<?php endif; ?>
<span style="display: none;"><?php echo ($pathDir !== ".")?stringUtils::lshorten($pathDir . '/', $maxLength - strlen($filename)):''; ?></span><?php echo stringUtils::lshorten($filename, $maxLength); ?>
<a
class="tooltip"
href="<?php echo url_for("default/file?" . http_build_query(array_merge($defaultParametersUrlFile, array('file' => $file['Id'])))); ?>"
title="<?php echo stringUtils::trimTicketInfos($file['LastChangeCommitDesc'])?>">
<span style="display: none;"><?php echo ($pathDir !== ".")?stringUtils::lshorten($pathDir . '/', $maxLength - strlen($filename)):''; ?></span><?php echo stringUtils::lshorten($filename, $maxLength); ?>
</a>
<?php if ($file['IsBinary']):?>
</a>
<span class="ricon binary" title="Binary file">Ñ</span>
<?php endif; ?>
</h3>
</td>
Expand Down
15 changes: 14 additions & 1 deletion apps/front/modules/default/templates/fileSuccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,20 @@ class="add_bubble
</tbody>
</table>
<?php else: ?>
<div class="flashMessage error">This is a binary file.</div>
<div>
<div class="imageDiff old">
<?php if (!$oldImageExists): ?><div class="flashMessage notice">No binary file in this revision.</div>
<?php elseif (is_null($oldImageType)): ?><div class="flashMessage error">This is an unknown type binary file.</div>
<?php else: ?><img src="data:image/<?php echo $oldImageType ?>;base64,<?php echo $oldImageContent ?>" title="<?php echo $file->getFilename() ?>" />
<?php endif;?>
</div>
<div class="imageDiff new">
<?php if (!$newImageExists): ?><div class="flashMessage notice">No binary file in this revision.</div>
<?php elseif (is_null($newImageType)): ?><div class="flashMessage error">This is an unknown type binary file.</div>
<?php else: ?><img src="data:image/<?php echo $newImageType ?>;base64,<?php echo $newImageContent ?>" title="<?php echo $file->getFilename() ?>" />
<?php endif;?>
</div>
</div>
<?php endif; ?>
</div>
<div id="comment_component" class="comments_holder">
Expand Down
60 changes: 48 additions & 12 deletions lib/git/GitCommand.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public function getDiffFilesFromBranch($gitDir, $referenceCommit, $lastCommit, $

if($withDetails)
{
$lineResults = $this->exec("git --git-dir=%s diff %s..%s --numstat | sed 's/\'$'\t''/ /g'", array($gitDir, $referenceCommit, $lastCommit));
$lineResults = $this->exec("git --git-dir=%s diff %s..%s --numstat", array($gitDir, $referenceCommit, $lastCommit));

$linesInfos = array();
foreach($lineResults as $line)
{
$infos = explode(' ', $line);
$infos = explode("\t", $line);
if(count($infos) == 3)
{
$linesInfos[$infos[2]] = array($infos[0], $infos[1]);
Expand Down Expand Up @@ -117,9 +117,7 @@ public function getShowFile($gitDir, $currentCommit, $filename)
{
$this->fetch($gitDir);

$fileContent = $this->exec('git --git-dir=%s show %s:%s', array($gitDir, $currentCommit, $filename));

return implode(PHP_EOL, $fileContent);
return $this->execReturnRaw('git --git-dir=%s show %s:%s', array($gitDir, $currentCommit, $filename));
}

/**
Expand All @@ -137,27 +135,25 @@ public function getShowFileFromBranch($gitDir, $referenceCommit, $currentCommit,
$gitDiffOptions = array(
'-U9999'
);

if (isset($options['ignore-all-space']) && $options['ignore-all-space'])
{
$gitDiffOptions[] = '-w';
}

$currentContentLinesResults = $this->exec('git --git-dir=%s diff '.implode(' ', $gitDiffOptions).' %s..%s -- %s', array($gitDir, $referenceCommit, $currentCommit, $filename));

$patternFinded = false;
$fileLines = $currentContentLinesResults;

foreach($currentContentLinesResults as $key => $currentContentLinesResult)
{
if($patternFinded === false)
unset($fileLines[$key]);
if(substr($currentContentLinesResult, 0, 2) == "@@")
{
unset($fileLines[$key]);
if(substr($currentContentLinesResult, 0, 2) == "@@")
{
break;
}
break;
}
}

return $fileLines;
}

Expand Down Expand Up @@ -233,4 +229,44 @@ public function exec($cmd, array $arguments = array(), &$status = null)

return $internOutput;
}

/**
* Executes a command, and returns the raw output as a string
* Absolutely no operation is done on the command's output.
*
* @param string $cmd
* @param array $arguments
* @param int & $status
*
* @return string
*/
public function execReturnRaw($cmd, array $arguments = array(), &$status = null)
{
$arguments = array_map('escapeshellarg', $arguments);

$cmd = vsprintf($cmd, $arguments);
$cmd.= ' 2>&1';

// exec() returns an array of strings, but quoting http://www.php.net/manual/en/function.exec.php :
// "Trailing whitespace, such as \n, is not included in this array."
// So, exec() will break the output -- especially when working with binary files.
// passthru() doesn't do that ; but writes on stdout ; which explains the need for output buffering.
ob_start();
passthru($cmd, $internStatus);
$internOutput = ob_get_contents();
ob_end_clean();

if($this->logger !== null)
{
$this->logger->log($cmd, $internStatus, $internOutput);
}

if(!is_null($status))
{
$status = $internStatus;
}

return $internOutput;
}

}
24 changes: 24 additions & 0 deletions lib/utils/ImageUtils.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

class ImageUtils
{
static public function getImageTypeFromContent($binaryContent)
{
$supportedTypes = array(
'jpeg' => "\xFF\xD8\xFF",
'gif' => 'GIF',
'png' => "\x89\x50\x4e\x47\x0d\x0a",
'bmp' => 'BM',
);

foreach ($supportedTypes as $supportedType => $header)
{
if (strpos($binaryContent, $header) === 0)
{
return $supportedType;
}
}

return null;
}
}
11 changes: 11 additions & 0 deletions web/css/less/_data.less
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,15 @@
}
}
}

.imageDiff {
float: left;
margin: 20px;
width: 46%;

img {
border: 1px solid #f9a;
max-width: 100%;
}
}
}
4 changes: 4 additions & 0 deletions web/css/less/_list.less
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@
color: #4a3;
font-size: 21px;
margin-right: 3px;

&.binary {
color: @lightGrey;
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions web/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@
font-size:21px;
margin-right:3px;
}
.list .list_body table tr td.file_name .ricon.binary { color:#999999; }
.list .list_body table tr td.branch_name { white-space:nowrap; }
.list .list_body table tr td.branch_name .new {
background-color:#4183c4;
Expand Down Expand Up @@ -486,6 +487,15 @@
font-size:12px;
}
.list_body.data table .line_comment .clipper { width:925px; }
.list_body.data .imageDiff {
float:left;
margin:20px;
width:46%;
}
.list_body.data .imageDiff img {
border:1px solid #ff99aa;
max-width:100%;
}
.form {
display:inline-block;
width:auto;
Expand Down