GithubHelp home page GithubHelp logo

dress's People

Contributors

nanhupatar avatar

Stargazers

 avatar  avatar  avatar

dress's Issues

批量获取dress图片并且进行格式封装

const fs = require("fs");
const path = require("path");
const whiteList = [".git"];
const config = {
  env: "test-f726fb",
  storage: "7465-test-f726fb",
  dir: "dress/"
};

//解析需要遍历的文件夹,我这以E盘根目录为例

//调用文件遍历方法
init();
function init() {
  let filePath = path.resolve("D:/Dress");
  let list = [];
  let files = fs.readdirSync(filePath);
  files.forEach(function(filename, index) {
    let fullname = path.join(filePath, filename);
    let stats = fs.statSync(fullname);
    if (stats.isDirectory() && !whiteList.includes(filename)) {
      let dressInfo = {
        userInfo: {
          nickName: filename,
          avatarUrl: ""
        },
        images: [],
        dressFlag: 1,
        description: "",
        star: 0,
        share: 0,
        tags: [],
        createTime: new Date().getTime()
      };

      list[index] = dressInfo;
      getFile(fullname, filename, list[index].images);
    }
  });

  list.forEach((item, index) => {
    let dressInfo = JSON.stringify(item) + "\r\n\r\n";
    fs.writeFile("dress.json", dressInfo, { flag: "a" }, err => {
      if (err) throw err;
      // console.log("saved," + dressInfo);
    });
  });
}

function getFile(filePath, nickName, images) {
  let files = fs.readdirSync(filePath);
  files.forEach((filename, index) => {
    let fullname = path.join(filePath, filename);
    let stats = fs.statSync(fullname);

    if (stats.isFile() && isImage(filename)) {
      let name = generateName(nickName, filename);
      images.push(name);
    }

    if (stats.isDirectory() && !whiteList.includes(filename)) {
      getFile(fullname, nickName, images);
    }
  });
}

function isImage(filename) {
  return /\.(png|jpe?g|gif|svg)(\?.*)?$/.test(filename);
}

function generateName(nickName, filename) {
  // cloud://test-f726fb.7465-test-f726fb/dress/60heye/IMG_3385.jpg
  return "cloud://" + config.env + "." + config.storage + "/" + config.dir + nickName + "/" + filename;
}

读取dress项目目录

var fs = require("fs");
var path = require("path");

//解析需要遍历的文件夹,我这以E盘根目录为例
var filePath = path.resolve("D:/Dress");

//调用文件遍历方法
fileDisplay(filePath);

/**
 * 文件遍历方法
 * @param filePath 需要遍历的文件路径
 */
function fileDisplay(filePath) {
  //根据文件路径读取文件,返回文件列表
  fs.readdir(filePath, function(err, files) {
    if (err) {
      console.warn(err);
    } else {
      //遍历读取到的文件列表
      files.forEach(function(filename) {
        //获取当前文件的绝对路径
        var filedir = path.join(filePath, filename);
        //根据文件路径获取文件信息,返回一个fs.Stats对象
        fs.stat(filedir, function(eror, stats) {
          if (eror) {
            console.warn("获取文件stats失败");
          } else {
            var isFile = stats.isFile(); //是文件
            var isDir = stats.isDirectory(); //是文件夹
            if (isFile) {
              console.log(filename);
              let obj = JSON.stringify({ filename: filename });
              obj += "\n";
              fs.writeFile(
                "input.json",
                obj,
                {
                  flag: "a"
                },
                function(err) {
                  if (err) {
                    return console.error(err);
                  }
                  // fs.readFile("input.txt", function(err, data) {
                  //   if (err) {
                  //     return console.error(err);
                  //   }
                  //   // console.log("异步读取文件数据: " + data.toString());
                  // });
                }
              );
            }
            if (isDir) {
              fileDisplay(filedir); //递归,如果是文件夹,就继续遍历该文件夹下面的文件
            }
          }
        });
      });
    }
  });
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.