/*
UploadTaskFile adds a file to a task

The file can't be larger than **5MB**
*/
171049
func (a *Client) UploadTaskFile(params *UploadTaskFileParams, authInfo runtime.ClientAuthInfoWriter) (*UploadTaskFileCreated, error) {
	// TODO: Validate the params before sending
	if params == nil {
		params = NewUploadTaskFileParams()
	}

	result, err := a.transport.Submit(&runtime.ClientOperation{
		ID:                 "uploadTaskFile",
		Method:             "POST",
		PathPattern:        "/tasks/{id}/files",
		ProducesMediaTypes: []string{"application/vnd.goswagger.examples.task-tracker.v1+json"},
		ConsumesMediaTypes: []string{"multipart/form-data"},
		Schemes:            []string{"http", "https"},
		Params:             params,
		Reader:             &UploadTaskFileReader{formats: a.formats},
		AuthInfo:           authInfo,
		Context:            params.Context,
		Client:             params.HTTPClient,
	})
	if err != nil {
		return nil, err
	}
	return result.(*UploadTaskFileCreated), nil

}
354720
func (c oauthClient) CreateFile(uploadID uint, taskID uint) (wl.File, error) {
	if uploadID == 0 {
		return wl.File{}, errors.New("uploadID must be > 0")
	}

	if taskID == 0 {
		return wl.File{}, errors.New("taskID must be > 0")
	}

	body := []byte(fmt.Sprintf(`{"upload_id":%d,"task_id":%d}`, uploadID, taskID))

	url := fmt.Sprintf("%s/files", c.apiURL)

	req, err := c.newPostRequest(url, body)
	if err != nil {
		return wl.File{}, err
	}

	resp, err := c.do(req)
	if err != nil {
		return wl.File{}, err
	}
	if err != nil {
		return wl.File{}, err
	}

	if resp.StatusCode != http.StatusCreated {
		return wl.File{}, fmt.Errorf("Unexpected response code %d - expected %d", resp.StatusCode, http.StatusCreated)
	}

	file := wl.File{}
	err = json.NewDecoder(resp.Body).Decode(&file)
	if err != nil {
		return wl.File{}, err
	}
	return file, nil
}
1107327
func createUploadStep(packagePath string) workflowStep {
	return func(ctx *workflowContext) (workflowStep, error) {
		defer recordDuration(time.Now(), "Uploading code", ctx)

		var uploadTasks []*workTask
		if len(ctx.userdata.lambdaAWSInfos) != 0 {
			// We always upload the primary binary...
			uploadBinaryTask := func() workResult {
				logFilesize("Lambda code archive size", packagePath, ctx.logger)

				// Create the S3 key...
				zipS3URL, zipS3URLErr := uploadLocalFileToS3(packagePath, "", ctx)
				if nil != zipS3URLErr {
					return newTaskResult(nil, zipS3URLErr)
				}
				ctx.context.s3CodeZipURL = newS3UploadURL(zipS3URL)
				return newTaskResult(ctx.context.s3CodeZipURL, nil)
			}
			uploadTasks = append(uploadTasks, newWorkTask(uploadBinaryTask))
		} else {
			ctx.logger.Info("Bypassing S3 upload as no Lambda functions were provided")
		}

		// We might need to upload some other things...
		if nil != ctx.userdata.s3SiteContext.s3Site {
			uploadSiteTask := func() workResult {
				tempName := fmt.Sprintf("%s-S3Site.zip", ctx.userdata.serviceName)
				tmpFile, err := system.TemporaryFile(ScratchDirectory, tempName)
				if err != nil {
					return newTaskResult(nil,
						errors.Wrapf(err, "Failed to create temporary S3 site archive file"))
				}

				// Add the contents to the Zip file
				zipArchive := zip.NewWriter(tmpFile)
				absResourcePath, err := filepath.Abs(ctx.userdata.s3SiteContext.s3Site.resources)
				if nil != err {
					return newTaskResult(nil, errors.Wrapf(err, "Failed to get absolute filepath"))
				}
				// Ensure that the directory exists...
				_, existsErr := os.Stat(ctx.userdata.s3SiteContext.s3Site.resources)
				if existsErr != nil && os.IsNotExist(existsErr) {
					return newTaskResult(nil,
						errors.Wrapf(existsErr,
							"TheS3 Site resources directory (%s) does not exist",
							ctx.userdata.s3SiteContext.s3Site.resources))
				}

				ctx.logger.WithFields(logrus.Fields{
					"S3Key":      path.Base(tmpFile.Name()),
					"SourcePath": absResourcePath,
				}).Info("Creating S3Site archive")

				err = spartaZip.AddToZip(zipArchive, absResourcePath, absResourcePath, ctx.logger)
				if nil != err {
					return newTaskResult(nil, err)
				}
				errClose := zipArchive.Close()
				if errClose != nil {
					return newTaskResult(nil, errClose)
				}

				// Upload it & save the key
				s3SiteLambdaZipURL, s3SiteLambdaZipURLErr := uploadLocalFileToS3(tmpFile.Name(), "", ctx)
				if s3SiteLambdaZipURLErr != nil {
					return newTaskResult(nil,
						errors.Wrapf(s3SiteLambdaZipURLErr, "Failed to upload local file to S3"))
				}
				ctx.userdata.s3SiteContext.s3UploadURL = newS3UploadURL(s3SiteLambdaZipURL)
				return newTaskResult(ctx.userdata.s3SiteContext.s3UploadURL, nil)
			}
			uploadTasks = append(uploadTasks, newWorkTask(uploadSiteTask))

		}

		// Run it and figure out what happened
		p := newWorkerPool(uploadTasks, len(uploadTasks))
		_, uploadErrors := p.Run()

		if len(uploadErrors) > 0 {
			return nil, errors.Errorf("Encountered multiple errors during upload: %#v", uploadErrors)
		}
		return validateSpartaPostconditions(), nil
	}
}
1237511
func SyncFile(localPath string, remote string, timeout int) error {
	fileInfo, err := os.Stat(localPath)
	if err != nil {
		log.Errorf("Failed to get size of source file: %s, err: %s", localPath, err)
		return err
	}
	fileSize := fileInfo.Size()
	directIO := (fileSize%Blocks == 0)
	log.Infof("source file size: %d, setting up directIo: %v", fileSize, directIO)

	var fileIo FileIoProcessor
	if directIO {
		fileIo, err = NewDirectFileIoProcessor(localPath, os.O_RDONLY, 0)
	} else {
		fileIo, err = NewBufferedFileIoProcessor(localPath, os.O_RDONLY, 0)
	}
	if err != nil {
		log.Error("Failed to open local source file:", localPath)
		return err
	}
	defer fileIo.Close()

	client := &syncClient{remote, timeout, localPath, fileSize, fileIo}

	defer client.closeServer() // kill the server no matter success or not, best effort

	err = client.syncFileContent(fileIo, fileSize)
	if err != nil {
		log.Errorf("syncFileContent failed: %s", err)
		return err
	}

	return err
}
711444
func (c *CheckpointWriter) UpdateTask(taskID string, status workflowpb.TaskState, err error) error {
	c.mu.Lock()
	defer c.mu.Unlock()

	errorMessage := ""
	if err != nil {
		errorMessage = err.Error()
	}

	t := c.checkpoint.Tasks[taskID]
	t.State = status
	t.Error = errorMessage
	return c.saveLocked()
}
1247247
func (s *storageImpl) registerTasks() {
	// See queue.yaml for "cas-uploads" task queue definition.
	s.tq.RegisterTask(&tasks.VerifyUpload{}, func(c context.Context, m proto.Message) error {
		return s.verifyUploadTask(c, m.(*tasks.VerifyUpload))
	}, "cas-uploads", nil)
	s.tq.RegisterTask(&tasks.CleanupUpload{}, func(c context.Context, m proto.Message) error {
		return s.cleanupUploadTask(c, m.(*tasks.CleanupUpload))
	}, "cas-uploads", nil)
}
404467
func (s *Service) UpdateTask(ctx context.Context, id influxdb.ID, upd influxdb.TaskUpdate) (*influxdb.Task, error) {
	var t *influxdb.Task
	err := s.kv.Update(ctx, func(tx Tx) error {
		task, err := s.updateTask(ctx, tx, id, upd)
		if err != nil {
			return err
		}
		t = task
		return nil
	})
	if err != nil {
		return nil, err
	}

	return t, nil
}
1296346
func TaskTrack(ctx types.Context) *types.Task {
	return getTaskService(ctx).TaskTrack(ctx)
}
1081192
func (c *Communicator) Upload(dst string, src io.Reader, fi *os.FileInfo) error {
	if fi == nil {
		return c.uploadReader(dst, src)
	}
	return c.uploadFile(dst, src, fi)
}
1260793
func (c oauthClient) UploadFile(
	localFilePath string,
	remoteFileName string,
	contentType string,
	md5sum string,
) (wl.Upload, error) {
	fileContents, err := c.readLocalFile(localFilePath)
	if err != nil {
		return wl.Upload{}, err
	}

	initialUploadResp, err := c.createUpload(remoteFileName, contentType, len(fileContents), md5sum)
	uploadID := initialUploadResp.ID

	// Upload actual data using returned URL
	// Do not worry about multi-part upload for now
	err = c.uploadAPart(initialUploadResp.Part, fileContents)
	if err != nil {
		return wl.Upload{}, err
	}

	return c.finishUpload(uploadID)
}
901135
func (c *Client) UploadFile(ctx context.Context, file string, u *url.URL, param *Upload) error {
	if param == nil {
		p := DefaultUpload // Copy since we set ContentLength
		param = &p
	}

	s, err := os.Stat(file)
	if err != nil {
		return err
	}

	f, err := os.Open(filepath.Clean(file))
	if err != nil {
		return err
	}
	defer f.Close()

	param.ContentLength = s.Size()

	return c.Upload(ctx, f, u, param)
}
730054
func UploadFile(c *middleware.Context) {
	if c.Req.Method == "POST" {
		// 32MiB
		e := c.Req.ParseMultipartForm(32 << 30)
		if e != nil {
			color.Red("%s", e)
			return
		}

		file, info, e := c.Req.FormFile("file")
		if e != nil {
			color.Red("%s", e)
			return
		}
		defer file.Close()

		bts, err := ioutil.ReadAll(file)
		if err != nil {
			c.Error(200, err.Error())
			return
		}

		f, err := models.Upload(info.Filename, bts)
		if err != nil {
			c.Error(200, err.Error())
			return
		}

		c.Data["uploaded"] = f
		c.Redirect("/files/upload/new?uploaded=" + fmt.Sprint(f.BlobID))
		return
	}

	if id := c.QueryInt("uploaded"); id != 0 {
		c.Data["uploaded"] = id

	}
	c.HTML(200, "files/new")
}
53481
func (server *Server) SendTask(signature *tasks.Signature) (*result.AsyncResult, error) {
	return server.SendTaskWithContext(context.Background(), signature)
}
1157001
func (o *UploadTaskFileParams) WithHTTPClient(client *http.Client) *UploadTaskFileParams {
	o.SetHTTPClient(client)
	return o
}
536056
func AddTask(t *automationpb.TaskContainer, taskName string, parameters map[string]string) {
	t.ParallelTasks = append(t.ParallelTasks, NewTask(taskName, parameters))
}
453104
func (api *Client) UploadFile(params FileUploadParameters) (file *File, err error) {
	return api.UploadFileContext(context.Background(), params)
}
118186
func (p *WebPage) UploadFile(selector, filename string) error {
	return p.ref.process.doJSON("POST", "/webpage/UploadFile", map[string]interface{}{"ref": p.ref.id, "selector": selector, "filename": filename}, nil)
}