UNPKG

2.64 kBJavaScriptView Raw
1"use strict";
2var mime = require("mime-types"), fileExtension = require("file-extension"), node_path = require("path"), fs = require("fs");
3var File = (function () {
4 /**
5 * File constructor
6 * @param e
7 * @param path
8 */
9 function File(e, path) {
10 this.path = path;
11 this.basename = node_path.basename(this.path);
12 this.dirname = node_path.dirname(this.path);
13 // verify path leads to a valid, readable file, handle error if not
14 this.getStatistics();
15 }
16 /**
17 * Refresh the file statistics after a rename or modification.
18 */
19 File.prototype.getStatistics = function () {
20 this.contentType = mime.lookup(this.getPath());
21 this.extension = fileExtension(this.getPath());
22 };
23 /**
24 * Get the basename.
25 * @returns {string}
26 */
27 File.prototype.getName = function () {
28 return this.basename;
29 };
30 /**
31 * Set a new file name.
32 * @param filename
33 */
34 File.prototype.setName = function (filename) {
35 this.basename = filename;
36 };
37 /**
38 * Get the file name of the job without the file extension.
39 * @returns {string}
40 */
41 File.prototype.getNameProper = function () {
42 return node_path.basename(this.getBasename(), node_path.extname(this.getBasename()));
43 };
44 /**
45 * Get the top level directory name.
46 * @returns {string}
47 */
48 File.prototype.getDirname = function () {
49 return this.dirname;
50 };
51 /**
52 * Get the complete directory path.
53 * @returns {string}
54 */
55 File.prototype.getPath = function () {
56 return this.path;
57 };
58 /**
59 * Set the complete directory path.
60 * @param path
61 */
62 File.prototype.setPath = function (path) {
63 this.path = path;
64 };
65 /**
66 * Get the content-type of the file.
67 * @returns {string}
68 */
69 File.prototype.getContentType = function () {
70 return this.contentType;
71 };
72 /**
73 * Get the file extension.
74 * @returns {string}
75 */
76 File.prototype.getExtension = function () {
77 return this.extension;
78 };
79 /**
80 * Get the basename.
81 * @returns {string}
82 */
83 File.prototype.getBasename = function () {
84 return this.basename;
85 };
86 /**
87 * Renames the local job file to the current name.
88 */
89 File.prototype.renameLocal = function () {
90 var new_path = this.getDirname() + node_path.sep + this.getName();
91 fs.renameSync(this.getPath(), new_path);
92 this.setPath(new_path);
93 this.getStatistics();
94 };
95 return File;
96}());
97exports.File = File;