I would like to send my file to the client (like android side) as a hash string(like base64 or any encryption).
But I don't how can I read my file on my host.
for example URL of my file is:
example.com/assets/videos/test.mp4
You can crypt a file or String like this:
$encrypted = Crypt::encryptString('example.com/assets/videos/test.mp4');
And you can decrypt it like this:
$decrypted = Crypt::decryptString($encrypted);
Do not forget to use this at the header:
use Illuminate\Support\Facades\Crypt;
For more information I would recommend checking out the docs
Related
I'd like to parse a .csr file in order to retrieve a Common Name (CN) value.
I have a csr file with the following structure:
verify OK
Certificate Request:
Data:
Version: 0(0x0)
Subject: C=XX, ST=SSSSSSS, L=SSSSSS, O=CORPORATION X.X, OUT=XX, CN=IAM.YOUARE.COM
.....
I'd like to retrieve the CN value (in the example above "IAM.YOUARE.COM")
Once I have this value, I'd like to insert it in a .txt file next to the DNS.X field (please notice that it's important to respect the white space between the equal operator and the inserted value). The structure of the .txt field is shown below:
[xxxxx]
basicConstraints = CA:FALSE^M
extendedkeyUsage = serverAuth
subjectAltNAme = #alt_names
[alt_name]
DNS.X = IAM.YOUARE.COM
Up until now, I've been accessing the csr file using the command "openssl req -text - noout -verify -in XXX.csr" and copying the CN value manually. However, I now have a vast list of .csr files to "parse" and I was wondering whether you could give me some indications on how to automate this process.
Assuming that using php would be an option (while it's obvious that one can use php to create websites, it's maybe less known that php scripts can also be run from the command line, like shell scripts but with .
// Load and decode the csr file
$csrfiledata=file_get_contents("/path/to/file.csr");
$csr = openssl_csr_get_subject($csrfiledata);
// openssl_csr_get_subject('file://path/to/file.csr') also works
// if the file was read properly, $csr['CN'] will contain the value of CN
Then, assuming that you created a template called template.txt that contains
[alt_name]
DNS.X = $$CN$$
where $$CN$$ is a randomly-chosen placeholder for the CN value. It's now easy to substitute the value read from the csr file and write the result to a file:
$path_to_infile = 'template.txt';
$path_to_outfile = 'outfile.txt';
$file_contents = file_get_contents($path_to_infile);
$file_contents = str_replace('$$CN$$',$csr['CN'],$file_contents);
file_put_contents($path_to_outfile,$file_contents);
How to iterate over files with php is explained here
I'm writing code in Laravel 5 to periodically backup a MySQL database. My code thus far looks like this:
$filename = 'database_backup_'.date('G_a_m_d_y').'.sql';
$destination = storage_path() . '/backups/';
$database = \Config::get('database.connections.mysql.database');
$username = \Config::get('database.connections.mysql.username');
$password = \Config::get('database.connections.mysql.password');
$sql = "mysqldump $database --password=$password --user=$username --single-transaction >$destination" . $filename;
$result = exec($sql, $output); // TODO: check $result
// Copy database dump to S3
$disk = \Storage::disk('s3');
// ????????????????????????????????
// What goes here?
// ????????????????????????????????
I've seen solutions online that would suggest I do something like:
$disk->put('my/bucket/' . $filename, file_get_contents($destination . $filename));
However, for large files, isn't it wasteful to use file_get_contents()? Are there any better solutions?
There is a way to copy files without needing to load the file contents into memory using MountManager.
You will also need to import the following:
use League\Flysystem\MountManager;
Now you can copy the file like so:
$mountManager = new MountManager([
's3' => \Storage::disk('s3')->getDriver(),
'local' => \Storage::disk('local')->getDriver(),
]);
$mountManager->copy('s3://path/to/file.txt', 'local://path/to/output/file.txt');
You can always use a file resource to stream the file (advisable for large files) by doing something like this:
Storage::disk('s3')->put('my/bucket/' . $filename, fopen('path/to/local/file', 'r+'));
An alternative suggestion is proposed here. It uses Laravel's Storage facade to read the stream. The basic idea is something like this:
$inputStream = Storage::disk('local')->getDriver()->readStream('/path/to/file');
$destination = Storage::disk('s3')->getDriver()->getAdapter()->getPathPrefix().'/my/bucket/';
Storage::disk('s3')->getDriver()->putStream($destination, $inputStream);
You can try this code
$contents = Storage::get($file);
Storage::disk('s3')->put($newfile,$contents);
As Laravel document this is the easy way I found to copy data between two disks
Laravel has now putFile and putFileAs method to allow stream of file.
Automatic Streaming
If you would like Laravel to automatically manage
streaming a given file to your storage location, you may use the
putFile or putFileAs method. This method accepts either a
Illuminate\Http\File or Illuminate\Http\UploadedFile instance and will
automatically stream the file to your desired location:
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
// Automatically generate a unique ID for file name...
Storage::putFile('photos', new File('/path/to/photo'));
// Manually specify a file name...
Storage::putFileAs('photos', new File('/path/to/photo'), 'photo.jpg');
Link to doc: https://laravel.com/docs/5.8/filesystem (Automatic Streaming)
Hope it helps
Looking at the documentation the only way is using method put which needs file content. There is no method to copy file between 2 file systems so probably the solution you gave is at the moment the only one.
If you think about it, finally when copying file from local file system to s3, you need to have file content to put it in S3, so indeed it's not so wasteful in my opinion.
I solved it in the following way:
$contents = \File::get($destination);
\Storage::disk('s3')
->put($s3Destination,$contents);
Sometimes we don't get the data using $contents = Storage::get($file); - storage function so we have to give root path of the data using Laravel File instead of storage path using Storage.
Currently we have a method that returns a string with a formatted CSV file.
string = EXPORT.tickets
We need to upload this csv file to a ftp server like so
ftp = Net::FTP.new(server, username, password)
ftp.putbinaryfile(string)
however, the string variable is obviously a string, and not a binary file as the putbinaryfile method expects. I see two ways to do this,
convert the string variable to a file first using File
convert the string directly to a file with something like StringIO
Do these seem like viable options? If so, how would I approach doing this, thanks in advance!
EDIT:
Since the putbinaryfile method is looking for a file path rather than an actual file, it looks like my best best will be to create a File from the string variable. Can anyone give an example of how this can be accomplished?
After talking to another developer, he gave me this solution which I found to be a better for my situation, since the file did not exist already. It skips writing the string to a Tempfile and uses StringIO to upload it directly. His solution:
The Net::FTP#putbinaryfile method takes the name of a file on the local filesystem to copy to the remote filesystem. Now, if your data is in a string (and wasn't read from a file on the filesystem) then you may want to use Net::FTP#storbinary instead:
require 'stringio'
require 'net/ftp'
BLOCKSIZE = 512
data = StringIO.new("Hello, world!\n")
hostname = "ftp.domain.tld"
username = "username"
password = "password"
remote_filename = "something.txt"
Net::FTP.open(hostname, username, password) do |ftp|
# ...other ftp commands here...
ftp.storbinary("STOR #{remote_filename}", data, BLOCKSIZE)
# ...any other ftp commands...
end
The above avoids writing data that's not on disk to disk, just so you can upload it somewhere. However, if the data is already in a file on disk, you might as well just fix your code to reference its filename instead.
Something like this should cover most of the bases:
require 'tempfile'
temp_file = Tempfile.new('for_you')
temp_file.write(string)
temp_file.close
ftp.putbinaryfile(temp_file)
temp_file.unlink
Using Tempfile relieves you from a lot of issues regarding unique filename, threadsafeness, etc. Garbage collection will ensure your file gets deleted, even if putbinaryfile raises an exception or similar perils.
The uploaded file will get a name like for_you.23423423.423.423.4, both locally and on the remote server. If you want it to have a specific name on the remote server like 'daily_log_upload', do this instead:
ftp.putbinaryfile(temp_file, 'daily_log_upload')
It will still have a unique name for the local temp file, but you don't care about that.
Without having to use Image::Magick;, is there a way to output to the local file system an MSSQL Image string as a JPEG/PNG file.
The following in C# works very well, struggling to find the equivalent in Perl.
string base64string = "/9j/4AAQSkZJRgABAQAAAQABAAD/4QB..."; # This string shortened otherwise it would not fit
byte[] blob = Convert.FromBase64String(base64string);
File.WriteAllBytes(#"C:\image.jpg", blob);
Thanks.
Looks like you just need decode_base64 on your string. Then write it to the file opened with > and run binmode on handler. So your data will not be corrupted by new line character conversion.
I'm attempting to download files from a website that uses a CDN for distribution. The URLs on the download page all end with file.pdf but clicking on the link in a browser results in the download of a file with a descriptive file name (e.g. 'invoice1234.pdf'). Obviously parsing the URL to get the file name results in every file being named file.pdf - I would like to use the same file name that is used when downloading via the browser. My code looks something like this:
filename = File.basename(download.href)
agent.pluggable_parser.default = Mechanize::Download
agent.get(mov_download_link.href).save("#{path}/#{filename}")
agent.pluggable_parser.default = Mechanize::File
Any ideas would be appreciated!
That filename is probably in a header that looks like this:
{'content-disposition' => 'filename="invoice1234.pdf"'}
If so:
f = agent.get(mov_download_link.href)
filename = f.header['content-disposition'][/"(.*)"/, 1]
f.save("#{path}/#{filename}")