简体   繁体   English

Vapor 将文件上传到 DerivedData 而不是项目结构中的 Public 文件夹

[英]Vapor uploads files to DerivedData instead of the Public folder in the project structure

When I try to upload files via my Vapor server, it always uploads files into the DerivedData folder instead of the Public folder inside the project structure.当我尝试通过我的 Vapor 服务器上传文件时,它总是将文件上传到DerivedData文件夹,而不是项目结构内的Public文件夹。

I can verify that the file is created in the path, but the path is somewhere in DerivedData directory .. why?我可以验证文件是在路径中创建的,但是路径在DerivedData目录中的某个位置.. 为什么? How am I going to server such file when it's not in the project's Public folder?当它不在项目的Public中时,我将如何处理此类文件?

My upload code:我的上传代码:

func create(request: Request) async throws -> HTTPStatus {
    try CreateDogRequest.validate(content: request)
    let createDogRequest = try request.content.decode(CreateDogRequest.self)

    guard let userId = try request.auth.require(User.self).id else {
        throw Abort(.notFound)
    }

    let dogId = UUID()

    let directory = DirectoryConfiguration.detect()
    let publicFolder = "Public"

    let folderPath = URL(fileURLWithPath: directory.workingDirectory)
        .appendingPathComponent(publicFolder, isDirectory: true)
        .appendingPathComponent("dogProfiles", isDirectory: true)
        .appendingPathComponent(userId.uuidString, isDirectory: true)
        .appendingPathComponent(dogId.uuidString, isDirectory: true)

    print("Starting writing to path: \(folderPath)")

    let filePath = folderPath.appendingPathComponent("hello" + ".jpg", isDirectory: false)

    try FileManager.default.createDirectory(at: folderPath, withIntermediateDirectories: true)

    let data = Data(buffer: createDogRequest.dogImage.data)
    try data.write(to: filePath, options: .atomic)

    print("file uploaded at: \(filePath.relativePath)")

    return .ok
}

Now this shows that the file is uploaded here:现在这表明文件已上传到此处:

/Users/<USERNAME>/Library/Developer/Xcode/DerivedData/<PROJECT_HANDLE>/Build/Products/Debug/Public/dogProfiles/62C340CE-262B-4DE4-9E2A-99B3B3126BB6/hello.jpg

Why?为什么? How can I serve such file then?那我怎样才能提供这样的文件呢?

I debugged the DirectoryConfiguration class and the detect() method checks if the server is running via Xcode and it looks like this:我调试了DirectoryConfiguration类,并且detect()方法检查服务器是否通过 Xcode 运行,它看起来像这样:

    #if Xcode
    if workingDirectory.contains("DerivedData") {
        Logger(label: "codes.vapor.directory-config")
            .warning("No custom working directory set for this scheme, using \(workingDirectory)")
    }
    #endif

But the funny thing is, if I put a file inside the Resource folder to read data from, and use the DirectoryConfiguration 's detect() method and create a path to that file, it finds the file no problem ??!!!但有趣的是,如果我将一个文件放在Resource文件夹中以从中读取数据,并使用DirectoryConfigurationdetect()方法并创建该文件的路径,它会发现该文件没有问题 ??!!! Why?为什么? How?如何? :D That is a mystery to me :D 这对我来说是个谜

This is the code that I wrote for reading from the file:这是我为从文件中读取而编写的代码:

        let directory = DirectoryConfiguration.detect()
        let configDir = "Resources"

        let path = URL(fileURLWithPath: directory.workingDirectory)
            .appendingPathComponent(configDir, isDirectory: true)
            .appendingPathComponent("file.txt", isDirectory: false)
            .relativePath

What am I missing here?我在这里想念什么? How come the file put inside Resources folder gets read, but when I want to put something inside the Public folder it gets put inside DerivedData even tho I am using the same patter when creating the path ??为什么会读取放入Resources文件夹中的文件,但是当我想在Public文件夹中放入一些东西时,即使我在创建path时使用相同的模式,它也会放入DerivedData中?

Ooookay, once again, I tried to think differently and I googled a bit and found this post: Ooookay,再一次,我试图以不同的方式思考,我用谷歌搜索了一下,发现了这篇文章:

How to get path of root directory of your project folder in Perfect 2.0? Perfect 2.0如何获取项目文件夹根目录的路径?

and the answer from worthbak pointed to the conclusion that I should rather run the server from terminal using swift run and try if the file gets created, and guess what?来自worthbak的回答指出了我应该使用swift run从终端运行服务器并尝试是否创建文件的结论,你猜怎么着? It does!确实如此!

This also explains how the file I put in the Resources folder was read, because it is inside a migration that you run from guess where?这也解释了我放在Resources文件夹中的文件是如何被读取的,因为它位于您从猜测哪里运行的迁移中? .. the Terminal :) .. 终点站 :)

You need to set a custom working directory in Xcode so Vapor knows where to looks.您需要在 Xcode 中设置一个自定义工作目录,以便 Vapor 知道在哪里查找。 See https://docs.vapor.codes/getting-started/xcode/#custom-working-directory请参阅https://docs.vapor.codes/getting-started/xcode/#custom-working-directory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM