Magento 1.8.1: media directory bug if using symlink - magento

I am having trouble with my Magento install, in the CMS when I go to insert an image with the wysiwig editor the folder keeps opening repeatedly.
The folder structure should be:
- infortis
- brands
- fortis
- ultimo
But what I get is:
-infortis
-infortis
-infortis
-infortis
-infortis
And this just keeps repeating.
Magento Version 1.8.1. Any help appreciated.

I have found that the following edits makes it work as expected, and works with non-symlinked (dev) resources as well:
In the same class as mentiond Mage_Cms_Helper_Wysiwyg_Images, apply these patches:
# This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- <html>Images.php (<b>Today 4:14:50 PM</b>)</html>
+++ <html><b>Current File</b></html>
## -223,7 +223,7 ##
public function getCurrentUrl()
{
if (!$this->_currentUrl) {
- $path = str_replace(Mage::getConfig()->getOptions()->getMediaDir(), '', $this->getCurrentPath());
+ $path = str_replace(realpath(Mage::getConfig()->getOptions()->getMediaDir()), '', $this->getCurrentPath());
$path = trim($path, DS);
$this->_currentUrl = Mage::app()->getStore($this->_storeId)->getBaseUrl('media') .
$this->convertPathToUrl($path) . '/';
# This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- <html>Images.php (<b>f47f0ff</b>)</html>
+++ <html><b>Current File</b></html>
## -68,7 +68,7 ##
*/
public function getStorageRoot()
{
- return Mage::getConfig()->getOptions()->getMediaDir() . DS . Mage_Cms_Model_Wysiwyg_Config::IMAGE_DIRECTORY
+ return realpath(Mage::getConfig()->getOptions()->getMediaDir()) . DS . Mage_Cms_Model_Wysiwyg_Config::IMAGE_DIRECTORY
. DS;
}

Found the issue in Mage_Cms_Helper_Wysiwyg_Images::convertIdToPath
The core code is as follows.
public function convertIdToPath($id)
{
$path = $this->idDecode($id);
if (!strstr($path, $this->getStorageRoot())) {
$path = $this->getStorageRoot() . $path;
}
return $path;
}
And the fix is to use realpath when getting the storage root as follows.
public function convertIdToPath($id)
{
$path = $this->idDecode($id);
$realpath = $this->getStorageRoot();
if (is_link(rtrim($realpath,'/'))) {
$realpath = realpath($realpath);
}
if (!strstr($path, $realpath)) {
$path = $realpath . $path;
}
return $path;
}
So what we have done is to rewrite Mage_Cms_Helper_Wysiwyg_Images and use the updated converIdToPath function. I found the original solution on a German website, but that will break if say you have a dev system without links and another system with a link.

We recently ran into this issue as well and I wanted to share a little more info to save the next person some time. If you are running the Magento Enterprise Edition and have an active support agreement in place there is an official patch available. Just open a support ticket and request the patch directly. The patch name is "PATCH_SUPEE-2662_EE_1.13.1.0_v1".

Related

CI4 - Trying to move image but get error "could not move file php6WkH2s to /var/www/example.com/development.example.com/app_dir/public/ ()

I am trying to upload a file and move it to public/ folder. The file uploads without problem to writable folder, however, it is the moving to the public folder that has a problem.
Here is my code;
$update_post->move(ROOTPATH.'public/', $update_post.'.'.$fileType);
Path is correct. When I echo out echo ROOTPATH.'public/'; and then manually copy/paste, I do get to the destination directory.
Permissions correct. There are my permission on the public/ directory:
drwxr-xr-x 9 www-data www-data 4096 Jan 30 01:08 public
Any hints appreciated.
Reason:
It's because the move(string $targetPath, ?string $name = null, bool $overwrite = false) method's $name argument is invalid.
$update_post->move( ... , $update_post.'.'.$fileType);
Explanation:
Concatenating an class CodeIgniter\Files\File extends SplFileInfo instance calls the inherited SplFileInfo class's __toString() method which returns the path to the file as a string.
Note that it doesn't return the filename, which is what you're interested in.
Solution:
You should instead pass in the basename instead.
$update_post->move(
ROOTPATH . 'public/',
$update_post->getBasename()
);
Alternatively, since you're not changing the destination filename, it's cleaner to just not pass in the second parameter of the move(...) method. I.e:
$update_post->move(
ROOTPATH . 'public'
);
Addendum:
If you wish to change the destination filename to a new name, try this instead:
guessExtension()
Attempts to determine the file extension based on the trusted
getMimeType() method. If the mime type is unknown, will return null.
This is often a more trusted source than simply using the extension
provided by the filename. Uses the values in app/Config/Mimes.php to
determine extension:
$newFileName = "site_logo"; // New filename without suffixing it with a file extension.
$fileExtension = $update_post->guessExtension();
$update_post->move(
ROOTPATH . 'public',
$newFileName . (empty($fileExtension) ? '' : '.' . $fileExtension)
);
Notes:
The move(...) method returns a new File instance for the relocated file, so you must capture the result if the resulting location is needed: $newRelocatedFileInstance = $update_post->move(...);

'No such file or directory' error when using buildGoPackage in nix

I'm trying to build the hasura cli: https://github.com/hasura/graphql-engine/tree/master/cli with the following code (deps derived from dep2nix):
{ buildGoPackage, fetchFromGitHub }:
buildGoPackage rec {
version = "1.0.0-beta.2";
name = "hasura-${version}";
goPackagePath = "github.com/hasura/graphql-engine";
subPackages = [ "cli" ];
src = fetchFromGitHub {
owner = "hasura";
repo = "graphql-engine";
rev = "v${version}";
sha256 = "1b40s41idkp1nyb9ygxgsvrwv8rsll6dnwrifpn25bvnfk8idafr";
};
goDeps = ./deps.nix;
}
but I get the following errors after the post-installation fixup step:
find: '/nix/store/gkck68cm2z9k1qxgmh350pq3kwsbyn8q-hasura-cli-1.0.0-beta.2': No such file or directory.
What am I doing wrong here? For reference, I'm on macOS and using home-manager.
For anyone still wondering:
There are a couple of things to consider:
dep has been deprecated in favor of go modules
This is also reflected in Nix, as buildGoPackage is now legacy and moved to buildGoModule
There is already a hasura-cli package in nixpkgs. You can just use it with nix-shell -p hasura-cli

Laravel file upload creates folder with a temp file

I'm trying to get my application to upload files to "public/uploads/documentos/{the-id}" but can't figure out what's wrong with my current setup. Right now, if I upload a file called "myfile.pdf", it creates a folder named "myfile.pdf" and inside of it a *.tmp file. In my database, "{the-id}/myfile.pdf" is saved as the file url but this takes me to the folder view, when what I want is to see the file inside of it.
How can I change it so that when a document is upload, it creates the file "myfile.pdf" directly under "public/uploads/documentos/{the-id}" so that I can access it like that? {the-id} is a folder created based on the patient's id, so all documents belonging to them are saved to the same folder.
Controller
public function store(Request $request)
{
$this->validate($request, [
'tipo' => 'required|max:100',
'descripcion' => 'max:200',
'fecha_documento' => 'max:20',
'archivo' => 'required|mimes:doc,docx,pdf,jpg,png,jpeg',
'mascota_id' => 'required',
'mascota_num' => 'required',
]);
$documento = new Documento;
$documento->mascota_num = $request->input('mascota_num');
$documento->mascota_id = $request->input('mascota_id');
$documento->tipo = ucfirst($request->input('tipo'));
$documento->descripcion = ucfirst($request->input('descripcion'));
$documento->fecha_documento = $request->input('fecha_documento');
if($request->hasFile('archivo')) {
$archivo = $request->file('archivo');
$archivo_folder = public_path('/uploads/documentos/') . $request->input('mascota_num');
$archivo_nombre = $archivo->getClientOriginalName();
$archivo_url = $archivo_folder . '/' . $archivo_nombre;
if (file_exists($archivo_folder)) {
$archivo->move($archivo_folder . '/' . $archivo_nombre);
} else {
File::makeDirectory($archivo_folder, $mode = 0777, true, true);
$archivo->move($archivo_folder . '/' . $archivo_nombre);
}
$documento->archivo = $request->input('mascota_num') . '/' . $archivo_nombre;
$documento->save();
return redirect()->route('mascotas.show', [$request->input('mascota_num')])->with('message', 'Documento agregado exitosamente.');
}
}
How my uploaded files look with this code:
uploads/documentos/
-- 1
-- file.pdf /folder
-- random_name.tmp /file
-- 2
-- file.pdf /folder
-- random_name.tmp /file
-- otherfile.pdf /folder
-- random_name.tmp /file
}
What I want
uploads/documentos/
-- 1
-- myfile.pdf /file
-- 2
-- myfile.pdf /file
-- otherfile.pdf /file
Ideally, I'd like to keep the files private, so I tried to upload them to the storage folder with this code:
Storage::put('documentos/'.$request->input('mascota_num').'/', $archivo_nombre);
$documento->archivo = Storage::url('documentos/'.$request->input('mascota_num').'/'.$archivo_nombre);
but {the-id} folder wasn't created and the files weren't saved, all I got was:
storage/app/documentos/
-- 1 /file without extension
-- 2 /file without extension
I'd settle for making the first part work for now. Thanks in advance for your help.
After much trial and error, I managed to upload files to the storage folder of my application.
if($request->hasFile('archivo')) {
$archivo = $request->file('archivo');
Storage::disk('documentos')->put($request->input('mascota_num') . '/' . $archivo->getClientOriginalName(), File::get($archivo));
$documento->archivo = $request->input('mascota_num') . '/' . $archivo_nombre;
$documento->save();
}
Files are saved to /storage/app/public/documentos/ under the correct patient id folder and with the correct file name.
I haven't found a way to correctly link to the files from my show blade, Storage::url('documentos/'.$documento->archivo)) shows a 404 error page, even though the file path is correct. Hopefully I can get this working next.

How to change the loaction of compressing folder for css/js in magento

I am using potato compressor extension in my site to compress the css/js files. I am getting those css/js inside the media folder. But I want them in the skin folder. Is there a way to change the location of compressing folder in potato compressor.
Following steps worked fine for me:
1. In File Package.php:
app/code/local/Potato/Compressor/Model/Design/Package.php
function getMergedJsUrl($files):
Change:
return Mage::getBaseUrl('media', Mage::app()->getRequest()->isSecure()) . $filePath . '/' . $targetFilename;
To:
return Mage::getBaseUrl('skin', Mage::app()->getRequest()->isSecure()) . $filePath . '/' . $targetFilename;
function getMergedCssUrl($files):
Change:
$baseMediaUrl = Mage::getBaseUrl('media', $isSecure);
To:
$baseMediaUrl = Mage::getBaseUrl('skin', $isSecure);
2. In File Package.php:
app/code/core/Mage/Core/Model/Design/Package.php
Change 'media' to 'skin' at all locations.
3. In File Package.php:
app/code/local/Potato/Compressor/Helper/Data.php
function getRootCachePath()
Change:
return Mage::getBaseUrl('media') . self::MAIN_FOLDER;
To:
return Mage::getBaseDir('skin') . DS. self::MAIN_FOLDER;
function getRootCacheUrl()
Change:
return Mage::getBaseUrl('media') . self::MAIN_FOLDER;
To:
return Mage::getBaseUrl('skin') . self::MAIN_FOLDER;
NOTE IN case if you have override the Potato/Compressor/Model/Design/Package.php then update that method with this:
In File Package.php:
app/code/local/Your_Theme/Navigation/Model/Compressor/Design/Package.php
function getMergedJsUrl($files):
Change:
return Mage::getBaseUrl('media', Mage::app()->getRequest()->isSecure()) . $filePath . '/' . $targetFilename;
To:
return Mage::getBaseUrl('skin', Mage::app()->getRequest()->isSecure()) . $filePath . '/' . $targetFilename;
function getMergedCssUrl($files):
Change:
$baseMediaUrl = Mage::getBaseUrl('media', $isSecure);
To:
$baseMediaUrl = Mage::getBaseUrl('skin', $isSecure);
Clear your magento and browser cache and js/css cache. Check the url of po_compressor file path by going to source of your site code, that will change to skin/ instead of media/
There is a config option Cache Directory under JS/CSS Compressor settings.
It becomes visible after enabling whether Merge JavaScript files or Merge CSS files option.
There are three possible values:
Media
Skin
Js

Perl Image::OCR::Tesseract module on Windows

Anyone out there know of a graceful way to install the "Image::OCR::Tesseract" module on Windows? The module fails to install on Windows via CPAN due to a *NIX only module dependency called "LEOCHARRE::CLI". This module does not seem to be required to run "Image::OCR::Tesseract" itself.
I've managed to get the module working by first manually installing the dependency modules listed in the makefile.pl (except for "LEOCHARRE::CLI") and then by moving the module file to the correct directory structure under "C:\Perl\site\lib\Image\OCR". The final part of getting it to work was to alter the section of code that calls the ImageMagick and Tesseract executables from the command line to put quotes around the program names when the executables are called by module.
This works, but I'd really feel better about doing a PPM or CPAN install on a production system from a repo that works on Windows.
Never mind, I got it, though I can't decide what is the better solution.
To get the installer to work on Windows via the traditional "perl makefile.pl, make, make test, make install" routine requires an edit to the Makefile.pl script, including the missing Windows install module (Devel::AssertOS::MSWin32), and patch to AssertEXE.pm to use "File::Which" rather than the built in shell "which" command that Windows lacks. All this still requires that The "Image::OCR::Tesseract" be patched to put quotes around program names when executing "convert" and "tesseract" from the command line.
Given the number of steps involved to make the installer work on Windows, and the fact the module does not create a binary component for the module to link to, I'd say the best option for installing and getting the Tesseract module working on windows would be to first install the following binary packages:
ImageMagick
Link
Tesseract
http://code.google.com/p/tesseract-ocr/downloads/list
Next, locate your Perl module directory - on my system it is "C:\Perl\site\lib". Create a folder "Image", if you don't have one. Next, open the Image folder and create a folder called "OCR". Open the OCR folder. At this point, your path should be something along the lines of "C:\Perl\site\lib\Image\OCR". Create a new text file called "Tesseract.pm", and copy in the following content...
package Image::OCR::Tesseract;
use strict;
use Carp;
use Cwd;
use String::ShellQuote 'shell_quote';
use Exporter;
use vars qw(#EXPORT_OK #ISA $VERSION $DEBUG $WHICH_TESSERACT $WHICH_CONVERT %EXPORT_TAGS #TRASH);
#ISA = qw(Exporter);
#EXPORT_OK = qw(get_ocr get_hocr _tesseract convert_8bpp_tif tesseract);
$VERSION = sprintf "%d.%02d", q$Revision: 1.24 $ =~ /(\d+)/g;
%EXPORT_TAGS = ( all => \#EXPORT_OK );
BEGIN {
use File::Which 'which';
$WHICH_TESSERACT = which('tesseract');
$WHICH_CONVERT = which('convert');
if($^O=~m/MSWin/) {
$WHICH_TESSERACT='"'.$WHICH_TESSERACT.'"';
$WHICH_CONVERT='"'.$WHICH_CONVERT.'"';
}
$WHICH_TESSERACT or die("Is tesseract installed? Cannot find bin path to tesseract.");
$WHICH_CONVERT or die("Is convert installed? Cannot find bin path to convert.");
}
END {
scalar #TRASH or return;
if ( $DEBUG ){
print STDERR "Debug on, these are trash files:\n".join("\n",#TRASH) ;
}
else {
unlink #TRASH;
}
}
sub DEBUG { Carp::cluck("Image::OCR::Tesseract::DEBUG() deprecated") }
sub get_hocr {
my ($abs_image,$abs_tmp_dir,$lang)= #_;
-f $abs_image or croak("$abs_image is not a file on disk");
my $hocr="hocr";
if(defined $abs_tmp_dir){
-d $abs_tmp_dir or die("tmp dir arg $abs_tmp_dir not a dir on disk.");
$abs_image=~/([^\/]+)$/ or die("cant match filename in path arg '$abs_image'");
my $abs_copy = "$abs_tmp_dir/$1";
# TODO, what if source and dest are same, i want it to die
require File::Copy;
File::Copy::copy($abs_image, $abs_copy)
or die("cant make copy of $abs_image to $abs_copy, $!");
# change the image to get ocr from to be the copy
$abs_image = $abs_copy;
# since it's a copy. erase that on exit
push #TRASH, $abs_image;
}
my $tmp_tif = convert_8bpp_tif($abs_image);
push #TRASH, $tmp_tif; # for later delete
_tesseract($tmp_tif,$lang,$hocr) || '';
}
sub get_ocr {
my ($abs_image,$abs_tmp_dir,$lang)= #_;
-f $abs_image or croak("$abs_image is not a file on disk");
if(defined $abs_tmp_dir){
-d $abs_tmp_dir or die("tmp dir arg $abs_tmp_dir not a dir on disk.");
$abs_image=~/([^\/]+)$/ or die("cant match filename in path arg '$abs_image'");
my $abs_copy = "$abs_tmp_dir/$1";
# TODO, what if source and dest are same, i want it to die
require File::Copy;
File::Copy::copy($abs_image, $abs_copy)
or die("cant make copy of $abs_image to $abs_copy, $!");
# change the image to get ocr from to be the copy
$abs_image = $abs_copy;
# since it's a copy. erase that on exit
push #TRASH, $abs_image;
}
my $tmp_tif = convert_8bpp_tif($abs_image);
push #TRASH, $tmp_tif; # for later delete
_tesseract($tmp_tif,$lang) || '';
}
sub convert_8bpp_tif {
my ($abs_img,$abs_out) = (shift,shift);
defined $abs_img or die('missing image arg');
$abs_out ||= $abs_img.'.tmp.'.time().(int rand(9000)).'.tif';
my #arg = ( $WHICH_CONVERT, $abs_img, '-compress','none','+matte', $abs_out );
#die (join(" ", #arg));
system(#arg) == 0 or die("convert $abs_img error.. $?");
$DEBUG and warn("made $abs_out 8bpp tiff.");
$abs_out;
}
# people expect tesseract to automatically convert
*tesseract = \&_tesseract;
sub _tesseract {
my ($abs_image,$lang,$hocr) = #_;
defined $abs_image or croak('missing image path arg');
$abs_image=~/\.tif+$/i or warn("Are you sure '$abs_image' is a tif image? This operation may fail.");
#my #arg = (
# $WHICH_TESSERACT, shell_quote($abs_image), shell_quote($abs_image),
# (defined $lang and ('-l', $lang) ), '2>/dev/null'
#);
my $cmd =
( sprintf '%s %s %s',
$WHICH_TESSERACT,
shell_quote($abs_image),
shell_quote($abs_image)
) .
( defined $lang ? " -l $lang" : '' ) .
( defined $hocr ? " hocr" : '' ) .
" 2>/dev/null";
$DEBUG and warn "command: $cmd";
system($cmd); # hard to check ==0
my $txt = $abs_image.($hocr?".html":".txt");
unless( -f $txt ){
Carp::cluck("no text output for image '$abs_image'. (No text file '$txt' found on disk)");
return;
}
$DEBUG and warn "Found text file '$txt'";
my $content = (_slurp($txt) || '');
$DEBUG and warn("content length of text in '$txt' from image '$abs_image' is ". length $content );
push #TRASH, $txt;
$content;
}
sub _slurp {
my $abs = shift;
open(FILE,'<', $abs) or die("can't open file for reading '$abs', $!");
local $/;
my $txt = <FILE>;
close FILE;
$txt;
}
1;
__END__
#sub _force_imgtype {
# my $img = shift;
# my $type = shift;
# my $delete_original = shift;
# $delete_original ||=0;
#
#
# if($img=~/\.$type$/i){
# return $img;
# }
#
# my $img_out= $img;
# $img_out=~s/\.\w{1,5}$/\.$type/ or die("cant get file ext for $img");
#
#
#
#}
Save and close. Close the command line session and open a new one if you've had one open from before you did the ImageMagick and Tesseract binary installs. Test the module with the following script:
use Image::OCR::Tesseract;
my $image = 'SomeImageFileThatContainsText.jpg';
my $text = Image::OCR::Tesseract::get_ocr($image);
print "Text...\n";
print $text."\n";
print "Normal Exit\n";
exit;
That's it. Messy, I know, but there's no good way around the fact that the module installer really needs to be updated to support Windows (and other) systems even though the actual module code almost runs without modification. Really, if Tesseract and ImageMagick were installed to paths without spaces then the "Image::OCR::Tesseract" module code would not need any changes, but this minor tweak lets the supporting executables be installed anywhere, including the default locations.

Resources