Yii2 File Validation .tar.xz - file-upload

How can I limit file upload to .tar.xz extension only ?
Example (not working, only with xz):
[['file1'], 'file','extensions' => 'tar.xz']
So far I have only obtained success for non-composite extensions (zip, xz, etc)

As from version 2.0.36 of yii2, you probably do the following to allow composite extension validation:
[['file1'], 'file','extensions' => 'tar.xz', 'checkExtensionByMimeType' => false]
The option checkExtensionByMimeType is introduced if the mimetype of the extension is not compatible with the extension. for example, if you make a text file with an extension like ant.fox

Related

how to test carrierwave fog google in rspec with setting up the configuration

I have below configuration and I wanted to write TC for it in ruby. I am new to ruby and wanted to understand how we can set the configuration of Fog to point to mock and use it in test-case.
class TestUploader < CarrierWave::Uploader::Base
storage :fog
def fog_credentials
{
:provider => 'google',
:google_project =>'my project',
:google_json_key_location =>'myCredentialFile.json'
}
end
def fog_provider
'fog/google'
end
def fog_directory
'{#bucket-name}'
end
def store_dir
when :File
"#{file.getpath}/file"
when :audio
"#{file.getpath}/audio"
else
p " Invalid file "
end
end
end
class TestModel
mount_uploader :images, TestUploader
end
Could someone please assist me from configuring to write and execute the unit test on it with few example. Any help would be really appreciated.
From the test I did, I got the following sample code working with Google Cloud Storage using Fog gem:
require "fog/google"
# Uncomment the following line if you want to use Mock
#Fog.mock!
# Bucket name
bucket = "an-existing-bucket"
# Timestamp used as sample string
test = Time.now.utc.strftime("%Y%m%d%H%M%S")
connection = Fog::Storage.new({
:provider => "Google",
:google_project => "your-project",
:google_json_key_location => "path-to-key.json",
})
# Lists objects in a bucket
puts connection.list_objects(bucket)
#Creates new object
connection.put_object(bucket, test, test)
puts "Object #{test} was created."
It works in production, but fails using mock mode with the following error:
`not_implemented': Contributions welcome! (Fog::Errors::MockNotImplemented)
It seems that it is not implemented as shown at the put_object method definition in the documentation.
Also, this is said in this GitHub issue:
Closing issue. 1.0.0 is out, and we have no more mocks for json backed objects.
Credentials
As shown in the Fog's documentation, to configure Google Credentials you have to them as follows:
connection = Fog::Storage.new({
:provider => 'Google',
:google_storage_access_key_id => YOUR_SECRET_ACCESS_KEY_ID,
:google_storage_secret_access_key => YOUR_SECRET_ACCESS_KEY
})
Mock
In the GitHub - Fog::Google documentation, there is also a minimal config to integrate Fog with Carrierwave.
In order to use the Cloud Storage mock, you can use the following line:
Fog.mock!
connection = Fog::Storage.new(config_hash)
Provider Specific Resources
In the provider documentation section, you will find links to provider specific documentation and examples.
Community supported providers can get assistance by filing Github Issues on the appropriate repository.
Provider
Documentation
Examples
Support Type
Google
Documentation
fog-google-examples
Community
In order to maximize the benefits of open source, you are encouraged submit bugs to Github Issues
In this GitHub example, you could find an implementation for Google Cloud Storage.
Class List
At the RubyGems documentation for fog-google, you can find the class definitions and parameters. For example, the list_objects method:
#list_objects(bucket, options = {}) ⇒ Google::Apis::StorageV1::Objects
Lists objects in a bucket matching some criteria.
Parameters:
bucket (String) — Name of bucket to list
options (Hash) (defaults to: {}) — Optional hash of options
Options Hash (options):
:delimiter (String) — Delimiter to collapse objects under to emulate a directory-like mode
:max_results (Integer) — Maximum number of results to retrieve
:page_token (String) — Token to select a particular page of results
:prefix (String) — String that an object must begin with in order to be returned
:projection ("full", "noAcl") — Set of properties to return (defaults to “noAcl”)
:versions (Boolean) — If true, lists all versions of an object as distinct results (defaults to False)
Returns:
(Google::Apis::StorageV1::Objects)

Why FileExtensionContentTypeProvider no work with .min.js extension?

I have some css&js files in my project and I used the BuildBundlerMinifier NuGet package to minify and obfuscate them.
For example, the app.js will minify and obfuscate into app.min.js in the same directory.
Now I want the user can access the app.min.js but can't access the app.js.
I do this for I don't want anybody else to access the source code of my js.
Although someone still can get its source code from the app.min.js while I don't want them to get it easily.
I tried to use FileExtensionContentTypeProvider in Configure of startup.cs to achieve this:
var provider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider();
provider.Mappings.Remove(".js");
provider.Mappings.Add(new KeyValuePair<string, string>(".min.js", "application/javascript"));
app.UseStaticFiles(new StaticFileOptions()
{
ContentTypeProvider=provider
});
However, after it runs I can access neither app.js nor app.min.js.
What's wrong with my code?
Thank you.
The FileExtensionContentTypeProvider is only meant to provide a mapping from file extension to the correct MIME type. In order to retrieve the file extension from a file name, it will do the following:
private static string? GetExtension(string path)
{
int index = path.LastIndexOf('.');
if (index < 0)
return null;
return path.Substring(index);
}
It will take the very last part of the extension. So with app.min.js, the reported file extension will still be .js and not .min.js, and as such the mapping for .js will be required.
Modifying the MIME type mapping in order to disallow certain file extensions is probably not the best strategy. It would be better to modify the underlying file provider itself to handle that.
Alternatively, if you want to prevent access to non-minified JavaScript files, you could also split the middleware to conditionally prevent serving static files for any request to a path that ends with .js that is not a .min.js:
app.UseWhen(ctx => !ctx.Request.Path.HasValue
|| !ctx.Request.Path.Value.EndsWith(".js")
|| ctx.Request.Path.Value.EndsWith(".min.js"), app2 =>
{
app2.UseStaticFiles();
});

FPDM Merge Error extract_pdf_definition_value() does not support definition '/Type'

I'm trying to fill out my PDF document using FPDM with a script I found here:
http://www.fpdf.org/en/script/script93.php
First, keep in mind that I don't want to install any additional libraries on the shared hosting setup I am using. I would have to switch hosts to do this.
Here's what I've tried:
Downgrading my PDF template to Acrobat 5.x (v1.4)
Disabling compression and fast web mode
Using the paid version of PDFTK and the Advanced output params "%PDFTK% %PDFIN% output %PDFOUT%" to modify the template I'm using (this prevents the weird error about object streams)
Used variations of FDF files with a different method.
Successfully wrote out an HTML version of my PDF, but it didn't look like my template exactly
My code:
require('/libs/fpdm/fpdm.php');
$fields = array(
'dealer_name' => 'My name',
'dealer_address' => 'My address',
'dealer_phone' => 'My city',
);
$pdf = new FPDM('/some/pdf/filename.pdf');
$pdf->Load($fields, false);
$pdf->Merge();
$pdf->Output('someoutput.pdf', 'F');
The Error
The error that I cannot seem to find anywhere is the following:
<b>FPDF-Merge Error:</b> extract_pdf_definition_value() does not support definition '/Type'
The Question
Are there any other things I can do to my PDF or any other methods to make this work?
fpdm.php is quite old but can be fixed up. The problem is the global variables can't be accessed from within the class FPDM.
To resolve the issue, the global variables need to be moved inside the class as protected properties.
Step 1
Remove the following lines:
$FPDM_FILTERS=array(); //holds all supported filters
$FPDM_REGEXPS= array(
"/Type"=>"/\/Type\s+\/(\w+)$/",
"/Subtype" =>"/^\/Subtype\s+\/(\w+)$/"
);
Step 2
Put the following immediately after "class FPDM {", around line 65:
protected $FPDM_FILTERS = array(); //holds all supported filters
protected $FPDM_REGEXPS = array(
"/Type"=>"/\/Type\s+\/(\w+)$/",
"/Subtype" =>"/^\/Subtype\s+\/(\w+)$/"
);
Step 3
Delete all instances of global $FPDM_FILTERS; and global $FPDM_REGEXPS;.
Step 4
Update the remaining references (except the property declarations).
Change $FPDM_FILTERS to $this->FPDM_FILTERS
Change $FPDM_REGEXPS to $this->FPDM_REGEXPS
Step 5
To properly include filters from the filters/ directory, we need to move the includes into the class. Remove the require_once()s from the beginning of the file then add the following at the beginning of the constructor FPDM()
$FPDM_FILTERS = array();
//Major stream filters come from FPDI's stuff but I've added some :)
require_once("filters/FilterASCIIHex.php");
require_once("filters/FilterASCII85.php");
require_once("filters/FilterFlate.php");
require_once("filters/FilterLZW.php");
require_once("filters/FilterStandard.php");
$this->FPDM_FILTERS = $FPDM_FILTERS;

Basic usage of modules in puppet with hiera

I want to use puppet to manage some servers. Even after reading dozens of documentation pages, it is not clear to me how to use modules and how to use them with hiera. As first experiment I wanted a user "admin" to be created on one node and found this module -> https://github.com/camptocamp/puppet-accounts
My /etc/puppet/hiera.yaml looks as simple as this
---
:backends:
- yaml
:hierarchy:
- node/%{::fqdn}
- common
:yaml:
:datadir: /etc/puppet/hieradata
My /etc/puppet/hieradata/node/node1.example.com.yaml contains this
---
accounts::users:
admin:
uid: 1010
comment: admin
accounts::ssh_keys:
admin:
comment: ad
type: ssh-rsa
public: AAAAAAAAAAAAAA
This worked after I put this in my /etc/puppet/manifests/site.pp
hiera_include('classes')
class
{
'accounts':
ssh_keys => hiera_hash('accounts::ssh_keys', {}),
users => hiera_hash('accounts::users', {}),
usergroups => hiera_hash('accounts::usergroups', {}),
}
accounts::account
{
'admin':
}
Is this good practice? To me it feels wrong to put that stuff into site.pp since it gets messed up when I later use more modules. But where else to put it? I also don't understand how this separates data from logic, since I have data in both, node1.example.com.yaml and site.pp (admin). Some help would be great..
To understand what hiera is, you should think simply that Hiera is a DATABASE for puppet, a database of Variables/values and nothing more.
For a beginner I would suggest to focus on other parts of the system, like how to create modules! and how to manage your needs (without complexity) and then slowly build the "smart" recipes or the reusable ones...
Your puppet will first sick for a file called sites.pp (usually is on your main $confdir (puppet.conf variable. I am not going to mention environments it is for later.)
e path is /etc/puppet inside that directory, you have a directory manifests. There is the place for your sites.pp
usually a sites.pp structure is:
node default {
include *module*
include *module2*
}
node /server\.fqdn\.local/ {
include *module2*
include *module3*
}
this means that you have a default Node (if the node name doesn't fit any other node, will use the default, otherwise it will use the regex matching of the node FQDN in this case server.fqdn.local.
The modules (module, module2 and module3) are stored inside the $modulespath set on your puppet.conf. In our case i will use the: /etc/puppet/modules
the tree will look like:
/etc/puppet/modules/
/etc/puppet/modules/module/
/etc/puppet/modules/module/manifests/
/etc/puppet/modules/module/manifests/init.pp
/etc/puppet/modules/module2/
/etc/puppet/modules/module2/manifests/
/etc/puppet/modules/module2/manifests/init.pp
/etc/puppet/modules/module3/
/etc/puppet/modules/module3/manifests/
/etc/puppet/modules/module3/manifests/init.pp
About
classes: https://docs.puppetlabs.com/puppet/latest/reference/lang_classes.html
generally what i explained but from puppet labs: https://docs.puppetlabs.com/puppet/latest/reference/dirs_manifest.html
Please note that the example from the README
class { 'accounts':
ssh_keys => hiera_hash('accounts::ssh_keys', {}),
users => hiera_hash('accounts::users', {}),
usergroups => hiera_hash('accounts::usergroups', {}),
}
is catering to users of Puppet versions before 3.x which had no automatic parameter lookup. With a recent version, you should just use this manifest:
include accounts
Since the Hiera keys have appropriate names, Puppet will look them up implicitly.
This whole thing still makes no sense to me. Since I have to put
accounts::account
{
'admin':
}
in a manifest file to create that user, what for is hiera useful in this case? It doesn't separate data from logic. I have data in both, the .yaml file (ssh keys, other account data) and in a manifest file (the snippet above). By using hiera I expect to be able to create that user inside /etc/puppet/hieradata/node/node1.example.com.yaml but this is not the case. What is the right way to do this? What for is the example hiera file of this module useful? Wouldn't it be easier create an account the old style way in site.pp?

Play 2.1 : Access files on server

Let's suppose I have a folder on my server. I want to be able to access this folder from a URL using Play 2.1.
I really don't know how and I have been searching a lot on the Internet, to no avail.
Here is a folder in which there are files I want to access :
/home/user/myFiles
I'd like that when I type the following URL
localhost:9000/filesOnServer/filename
I download the file named "filename" in the folder myFiles.
This is not what I want to do :
GET /filesOnServer/*file controllers.Assets.at(path="/anything", file)
Indeed, this way I can only access files inside the play application directory.
Moreover, if I were to use dist, then the files are stored in a .jar and we can no longer add files to the application.
Thank you for your help.
Are you using scala or java?
I think you should look for Play.getFile() and SimpleResult()
Here is a sample of a little Controller method in scala, it might not be the most efficient but it seems to work !
def getFile() = Action {
val file = Play.getFile("../../Images/img.png")(Play.current)
val fileContent = Enumerator.fromFile(file)
SimpleResult(
header = ResponseHeader(200, Map(
CONTENT_LENGTH -> file.length.toString,
CONTENT_TYPE -> "image/png",
CONTENT_DISPOSITION -> s"attachment; filename=${file.getName}")),
body = fileContent)
}
Hope it will help you!
Note: you could also use new java.io.File(absolutePath)