How to get the public path in Perl Dancer - path

Probably a silly question, but how do I get the path to the /public folder in Dancer?
I want to store/read csv files under the public folder, but don't know if Dancer offers any convenience methods to get the base path to the public folder.
The error I get when trying to create a file by saying:
open(FILE, ">>", "myapp/public/file.csv") or die "$!";
is:
No such file or directory in /ur/share/perl5/Dancer/Handler.pm l. 98
I'm not sure why it's going to Handler.pm?

My first answer is, don't do it that way...for two reasons:
You're potentially opening up a security issue. What if somehow a hacker figures out a way to write to your environment files, change your passwords, etc.?
You really want your /public files to be static, for version control, portability, etc.
That being said, if you really still want to do this, the public dir lives in config->{public}:
print "Public dir:".config->{public}."\n";
Source:
http://search.cpan.org/~xsawyerx/Dancer-1.3110/lib/Dancer/Config.pm#public_%28directory%29

Related

Organize GraphQL files in directories in Rails

Upon running rails g graphql:install, a set of useful base type files are created in /app/graphql/types for us to extend when defining our own types. When running any of the generators, it creates these files in the same folder also. I set about creating sub directories thinking I could add some sense to this giant catch-all directory, but couldn't get things to load properly.
Since there is a base file for each type (base_enum.rb, base_object.rb, etc.), I created a folder for extensions of each of these types (enum_types, object_types, etc.). This broke auto loading though and I had to explicitly import these files to be able to use these custom types. So, at the top of query_type.rb, mutation_type.rb and app/graphql/mutations/base_mutation.rb I added the following:
['enum_typs', 'input_object_types', 'interface_types', 'object_types', 'scalar_types', 'union_types'].each do |dir|
Dir[File.dirname(__FILE__) + "/#{dir}/*.rb"].each {|file| require file }
end
This allowed things to run, but any change would break auto loading so I would have to restart the server on each change. I started reading through this article about auto loading on the rails site, but it was quite honestly a little over my head. Though it led me to believe I had to either find the correct names for my folders or namespace the objects defined in my type definition files properly to be able to do this.
Is there a sane way to organize these files in sub-directories which doesn't break auto loading? Do most projects just have a flat folder structure for these files?
Thank you!

Trying to read file but not finding the path?

Question/Answer Read a file in groovy into a string is simple, except I get a FileNotFound exception no matter where I move the file, e.g. even to the root directory on a linux box.
If I do:
String cpni = new File('/cpni_p').text
and the file cpni_p resides right in the root directory of the system (i.e. make it as simple as possible), I get a
FileNotFoundException: \cpni_p (the system cannot find the file specified)
How do I fix this? I'm open to direct Java too -- just want to get it to work.
There was a directory level privilege problem.

Lua - My documents path and file creation date

I'm planning to do a program with Lua that will first of all read specific files
and get information from those files. So my first question is whats the "my documents" path name? I have searched a lot of places, but I'm unable to find anything. My second question is how can I use the first four letters of a file name to see which one is the newest made?
Finding the files in "my documents" then find the newest created file and read it.
The reading part shouldn't be a problem, but navigating to "my documents" and finding the newest created file in a folder.
For your first question, depends how robust you want your script to be. You could use Lua's builtin os.getenv() to get a variety of environment vars related to user, such as USERNAME, USERPROFILE, HOMEDRIVE, HOMEPATH. Example:
username = os.getenv('USERNAME')
dir = 'C:\\users\\' .. username .. '\\Documents'
For the second question, there is no builtin mechanism in Windows to have the file creation or modification timestamp as part of the filename. You could read the creation or modification timestamp, via a C extension you create or using an existing Lua library like lfs. Or you could read the contents of a folder and parse the filenames if they were named according to the pattern you mention. Again there is nothing built into Lua to do this, you would either use os.execute() or lfs or, again, your own C extension module, or combinations of these.

How to compress multiple folders into one archive?

I have some compression components (like KAZip, JVCL, zLib) and exactly know how to use them to compress files, but i want to compress multiple folders into one single archive and keep folders structure after extract, how can i do it?
in all those components i just can give a list of files to compress, i can not give struct of folders to extract, there is no way (or i couldn't find) to tell every file must be extracted where:
i have a file named myText.txt in folder FOLDER_A and have a file with same name myText.txt in folder FOLDER_B:
|
|__________ FOLDER_A
| |________ myText.txt
|
|__________ FOLDER_B
| |________ myText.txt
|
i can give a list of files to compress: myList(myText.txt, myText.txt) but i cant give the structure for uncompress files, what is best way to found which file belongs to which folder?
The zip format just does not have folders. Well, it kinda does, but they are kind of empty placeholders, only inserted if you need metadata storage like user access rights. But other than those rather rare advanced things - there is no need for folders at all. What is really done - and what you can observe opening zip file in the notepad and scrolling to the end - is that each file has its path in it, starting with "archive root". In your exanple the zip file should have two entries (two files):
FOLDER_A/myText.txt
FOLDER_B/myText.txt
Note, that the separators used are true slashes, common to UNIX world, not back-slashes used in DOS/Windows world. Some libraries would fix back-slashes it for you, some would not - just do your tests.
Now, let's assume that that tree is contained in D:\TEMP\Project - just for example.
D:\TEMP\Project\FOLDER_A\myText.txt
D:\TEMP\Project\FOLDER_B\myText.txt
There are two more questions (other than path separators): are there more folders within D:\TEMP\Project\ that should be ignored, rather than zipped (like maybe D:\TEMP\Project\FOLDER_C\*.* ? and does your zip-library have direct API to pack the folders wit hall its internal subfolder and files or should you do it file by file ?
Those three questions you should ask yourself and check while choosing the library. The code drafts would be somewhat different.
Now let's start drafting for the libraries themselves:
The default variant is just using Delphi itself.
Enumerate the files in the folder: http://docwiki.embarcadero.com/CodeExamples/XE3/en/DirectoriesAndFilesEnumeraion_(Delphi)
If that enumeration results in absolute paths then strip the common D:\TEMP\Project from the beginning: something like If AnsiStartsText('D:\TEMP\Project\', filename) then Delete(filename, 1, Length('D:\TEMP\Project\'));. You should get paths relative to chosen containing place. Especially if you do not compress the whole path and live some FOLDER_C out of archive.
Maybe you should also call StringReplace to change '\' into '/' on filenames
then you can zip them using http://docwiki.embarcadero.com/Libraries/XE2/en/System.Zip.TZipFile.Add - take care to specify correct relative ArchiveFileName like aforementioned FOLDER_A/myText.txt
You can use ZipMaster library. It is very VCL-bound and may cause troubles using threads or DLLs. But for simple applications it just works. http://www.delphizip.org/
Last version page have links to "setup" package which had both sources, help and demos. Among demos there is an full-featured archive browser, capable of storing folders. So, you just can read the code directly from it. http://www.delphizip.org/191/v191.html
You talked about JVCL, that means you already have Jedi CodeLib installed. And JCL comes with a proper class and function, that judging by name can directly do what you want it too: function TJclSevenzipCompressArchive.AddDirectory(const PackedName: WideString; const DirName: string = ''; RecurseIntoDir: Boolean = False; AddFilesInDir: Boolean = False): Integer;
Actually all those libraries are rather similar on basic level, when i made XLSX export i just made a uniform zipping API, that is used with no difference what an actual zipping engine is installed. But it works with in-memory TStream rather than on-disk files, so would not help you directly. But i just learned than apart of few quirks (like instant vs postponed zipping) on ground level all those libs works the same.

Actionscript - combining AS2 assets into a single SWF

I have a flash project that I'm trying to export as a single SWF. There's a main SWF file that loads about 6 other SWFs, and both the main and the child SWFs reference other external assets (images, sounds, etc). I'd like to package everything as a single .swf file so I don't have to tote the other assets around with the .swf.
All the coding is done in the timeline, but the assets haven't been imported into the Flash Authoring environment and I don't have time to do that right now (there are too many references to them everywhere). I'm hoping that there's just an option I'm missing that allows this sort of packaged export, but I haven't found anything like that.
I don't have access to Flex or mxmlc (and as the AS is timeline-based, they wouldn't necessarily help me). Any thoughts?
Thanks!
PS...if there's no way of doing exactly what I'm saying, I could deal with having all the assets in a "assets" folder or something like that, so I'd just be toting around main.swf and an assets folder. The problem here is that all the references to the assets assume that they're in the same folder as the main.swf file, so everything's assumed to be local...is there a way to change the scope of all external references in Flash (so, for example, all local references in the code are actually searched in /assets)?
You might be able to decompile your swfs into XML with swfmill/mtasc and use a fancy XSLT to recombine them and recompile with swfmill/mtasc.
If that doesn't work and if you're using MovieClip.loadMovie or MovieClipLoader.loadMovie you can overload their methods and intercept the url:
var realLoadMovie:Function = MovieClip.prototype.loadMovie;
MovieClip.prototype.loadMovie = function(url:String, method:String) {
return realLoadMovie("assets/" + url, method);
}
var test:MovieClip = createEmptyMovieClip("testclip", getNextHighestDepth());
test.loadMovie("test.swf");
You'll need to do some additional string parsing if the urls have a resource-type prefix such as file://
There's a base parameter you can add when you embed the swf, just like align, scale, etc. If base is set, all relative urls will be prefixed with whatever path you define (well, almost all; videos and file reference objects being the exception here).
Other than that, I'd go with nikaji's solution.
HI Justin,
It sounds like you need to look into using shared libraries. Check out:
http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_14767

Resources